Anonymous avatar Anonymous committed 50f3dde

new pile-up evt loop mgr

Comments (0)

Files changed (2)

AthenaServices/PileUpEventLoopMgr.h

+//====================================================================
+//	PileUpEventLoopMgr.h
+//--------------------------------------------------------------------
+//
+//	Package    : AthenaServices
+//
+//  Description: definition of the pile-up processing manager
+//               service for the application manager
+//
+//	Author     : PC
+//====================================================================
+#ifndef STOREGATE_PILEUPEVENTLOOPMGR_H
+#define STOREGATE_PILEUPEVENTLOOPMGR_H
+
+#include <string>
+#include <vector>
+
+/* not on egcs #include <boost/function.hpp> */
+#include <boost/function.hpp>
+#include <boost/function/function1.hpp>
+// Framework include files
+#include "GaudiKernel/IEvtSelector.h"
+#include "GaudiKernel/Property.h"
+
+#include "GaudiKernel/SvcFactory.h"  /*FIXME should be in MinimalEvtLooMgr.h */
+#include "ApplicationMgr/MinimalEventLoopMgr.h"
+
+// Forward declarations
+class IIncidentSvc;
+class IEvtSelector;
+class StoreGateSvc;
+
+/** Class definition of PileUpEventLoopMgr.
+    This is the Atlas event loop for pile-up applications.
+    This object handles the minimal requirements needed by the 
+    application manager. It also handles the default user 
+    configuration setup for standard event processing.
+
+    <UL>
+    <LI> handling of the incidents when starting event processing
+    <LI> handling of the event loop
+    </UL>
+
+    <B>History:</B>
+    <PRE>
+    +---------+----------------------------------------------+---------+
+    |    Date |                 Comment                      | Who     |
+    +---------+----------------------------------------------+---------+
+    |13/12/00 | Initial version                              | M.Frank |
+    +---------+----------------------------------------------+---------+
+    </PRE>
+   @author Markus Frank
+   @version 1.0
+*/
+class PileUpEventLoopMgr : public MinimalEventLoopMgr   {
+public:
+  /// Creator friend class
+  friend class SvcFactory<PileUpEventLoopMgr>;
+  typedef IEvtSelector::Iterator EvtIterator;
+
+protected:
+  /// Standard Constructor
+  PileUpEventLoopMgr(const std::string& nam, ISvcLocator* svcLoc);
+  /// Standard Destructor
+  virtual ~PileUpEventLoopMgr();
+  /// setup input and overlay selectors and iters
+  StatusCode setupStreams();
+  StatusCode setupSelector(const std::string& selecType,
+			   const std::string& selecName,
+			   IEvtSelector*& pSel);
+  StatusCode setupStore(const std::string& storeName,
+			StoreGateSvc*& pSG);
+
+public:
+  /// implementation of IAppMgrUI::initialize
+  virtual StatusCode initialize();
+  /// implementation of IAppMgrUI::finalize
+  virtual StatusCode finalize();
+  /// implementation of IAppMgreUI::terminate
+  //  virtual StatusCode terminate();
+  /// implementation of IAppMgrUI::nextEvent
+  virtual StatusCode nextEvent(int maxevt);
+  /// implementation of IEventProcessor::executeEvent(void* par)
+  virtual StatusCode executeEvent(void* par);
+
+private:
+  /// Incident Service
+  IIncidentSvc* p_incidentSvc;
+
+  /// Input Selectors 
+  IEvtSelector* p_origSel;
+  std::vector<IEvtSelector*> m_bkgSelVec;
+  /// StoreGateSvcs;
+  StoreGateSvc* p_SGOver;              // overlaid (output) event store 
+  StoreGateSvc* p_SGOrig;              // original event store
+  std::vector<StoreGateSvc*> m_bkgSGSvcVec; //bkg event stores 
+  /// Input Iterators
+  EvtIterator* p_origIter;               
+  std::vector<EvtIterator*> m_bkgIterVec;
+
+  //Properties
+  /// Original (Physics) Event selector names.
+  StringProperty m_origSelName;
+  /// Event selector types. These will be used to instantiate the selecs
+  StringArrayProperty m_selecNames;
+  // number of selectors to instantiate from each stream
+  IntegerArrayProperty m_streamSelectors;  //FIXME not used yet
+  unsigned int m_nInputs;
+  unsigned int m_nStores;
+
+  /// Xing frequency(ns);
+  FloatProperty m_xingFreq;
+  /// first and last xing to be simulated (0th xing is 1st after trigger)
+  IntegerProperty m_firstXing;
+  IntegerProperty m_lastXing;
+  /// # of collisions/xings (~beam intensity)
+  FloatProperty m_collXing;
+  /// select collision distribution
+  StringProperty m_collDistrName;
+  //  boost::function<long> f_collDistr;   //function pointer
+  boost::function<long, double> f_collDistr;   //function pointer
+  //  long (*f_collDistr)(double);    //function pointer
+};
+#endif // STOREGATE_PILEUPEVENTLOOPMGR_H
+
+
+
+
+
+
+

src/PileUpEventLoopMgr.cxx

+//===================================================================
+//	PileUpEventLoopMgr.cpp
+//--------------------------------------------------------------------
+//
+//	Package    : PileUpEventLoopMgr
+//
+//  Description: IAppMgrUI implementation to steer pile-up
+//
+//	Author     : M.Frank
+//  History    :
+// +---------+----------------------------------------------+---------
+// |    Date |                 Comment                      | Who     
+// +---------+----------------------------------------------+---------
+// | 29/10/98| Initial version                              | MF
+// +---------+----------------------------------------------+---------
+// | 1/8/01  | Modified for use with StoreGate              | SR
+// +---------+----------------------------------------------+---------
+// | 12/9/01 | Modified for Pile-Up applications            | PC
+// +---------+----------------------------------------------+---------
+//
+//====================================================================
+#define  GAUDISVC_EVENTLOOPMGR_CPP
+
+#include "CLHEP/Random/RandPoisson.h"
+/* #include "CLHEP/Random/RanecuEngine.h" */
+namespace collDistr {
+  inline long fixed (double ave) { return (long) ave; }
+  inline long poisson (double ave) { return RandPoisson::shoot(ave); }
+}
+
+//  FIXME old gcc
+#include <strstream>
+#include "GaudiKernel/IAlgorithm.h"
+#include "GaudiKernel/SmartIF.h"
+#include "GaudiKernel/Incident.h"
+#include "GaudiKernel/MsgStream.h"
+#include "GaudiKernel/SvcFactory.h"
+#include "GaudiKernel/DataSelectionAgent.h"
+#include "GaudiKernel/DataObject.h"
+#include "GaudiKernel/IIncidentSvc.h"
+#include "GaudiKernel/IEvtSelector.h"
+#include "GaudiKernel/IDataManagerSvc.h"
+#include "GaudiKernel/IConversionSvc.h"
+#include "GaudiKernel/ISvcManager.h"
+#include "StoreGate/StoreGateSvc.h"
+
+#include "EventAthena/EventInfo.h"
+#include "AthenaServices/PileUpEventLoopMgr.h"
+#include "AthenaServices/CloneService.h"
+
+// Instantiation of a static factory class used by clients to create instances of this service
+static SvcFactory<PileUpEventLoopMgr> s_PileUpEventLoopMgrFactory;
+const ISvcFactory& PileUpEventLoopMgrFactory = s_PileUpEventLoopMgrFactory;
+
+
+namespace detail {
+  void splitSlash(const std::string& ID, std::string& type, std::string& name) 
+  {
+    std::string::size_type iSlash = ID.find('/');
+    type = ID.substr(0, iSlash);
+    name = ID.substr(iSlash+1);
+    if (name.empty()) name = type; //=ID
+  }
+}
+
+//=========================================================================
+// Standard Constructor
+//=========================================================================
+PileUpEventLoopMgr::PileUpEventLoopMgr(const std::string& nam, 
+				       ISvcLocator* svcLoc)
+  : MinimalEventLoopMgr(nam, svcLoc), m_nInputs(0), m_nStores(0), 
+  m_xingFreq(25), m_firstXing(-2), m_lastXing(+1), m_collXing(23.0),
+  m_collDistrName("Poisson")
+{
+  declareProperty("OrigSelector", m_origSelName);
+  declareProperty("BkgSelectors", m_selecNames);
+  declareProperty("StreamSelectors", m_streamSelectors);
+  declareProperty("XingFrequency", m_xingFreq);	// (ns)
+  declareProperty("firstXing", m_firstXing);    // time index (*m_xingFreq)
+  declareProperty("lastXing", m_lastXing);      // time index (*m_xingFreq)
+  declareProperty("CollPerXing", m_collXing);  // collisions per crossing
+  declareProperty("CollDistribution", m_collDistrName); // Poisson, Fixed
+}
+
+//=========================================================================
+// Standard Destructor
+//=========================================================================
+PileUpEventLoopMgr::~PileUpEventLoopMgr()   
+{  }
+
+//=========================================================================
+// implementation of IAppMgrUI::initialize
+//=========================================================================
+StatusCode PileUpEventLoopMgr::initialize()    
+{
+
+  MsgStream log(msgSvc(), name());
+
+  //locate the StoreGateSvc and initialize our local ptr
+  StatusCode sc = service("StoreGateSvc", p_SGOver);
+  if (!sc.isSuccess() || 0 == p_SGOver) {
+    log << MSG::ERROR << "Could not find StoreGateSvc" << endreq;
+    return sc;
+  }
+
+  //locate the IncidentSvc and initialize our local ptr
+  sc = serviceLocator()->service("IncidentSvc", p_incidentSvc);
+  if( !sc.isSuccess() )  
+  {
+    log << MSG::FATAL << "Error retrieving IncidentSvc." << endreq;
+    return sc;
+  }
+
+//-------------------------------------------------------------------------
+// Process Properties
+//-------------------------------------------------------------------------
+  try {
+    setProperties();
+  } catch (...) {
+    log << MSG::WARNING << "Caught exception thrown reading in properties" << endreq;
+  }
+  // select collision distribution function
+  if (m_collDistrName.value() == "Fixed") 
+    f_collDistr = &collDistr::fixed;      
+  else if (m_collDistrName.value() == "Poisson") {
+    //    RandPoisson poisson(new RanecuEngine(), m_collXing); //CLHEP
+    //    f_collDistr = poisson.fire;
+    f_collDistr = &collDistr::poisson;
+
+  } else {
+    log << MSG::ERROR << m_collDistrName 
+	<< " is not a know collision distribution function" << endreq;
+    return StatusCode::FAILURE;
+  }
+//-------------------------------------------------------------------------
+// Setup Event Selector
+//-------------------------------------------------------------------------
+  if (!(setupStreams().isSuccess()))
+  {
+    log << MSG::ERROR
+	<< "Failed to initialize pile-up streams"
+	<< endreq;
+    return sc;
+  }
+
+//-------------------------------------------------------------------------
+// Base class initialize (done at the end to allow algs to access stores)
+//-------------------------------------------------------------------------
+
+  return MinimalEventLoopMgr::initialize();
+}
+
+//=========================================================================
+// implementation of IAppMgrUI::finalize
+//=========================================================================
+StatusCode PileUpEventLoopMgr::finalize()    
+{
+  return MinimalEventLoopMgr::finalize();
+}
+
+//=========================================================================
+// implementation of IAppMgrUI::terminate
+//=========================================================================
+//  StatusCode PileUpEventLoopMgr::terminate() 
+//  {
+//     m_histoDataMgrSvc = releaseInterface(m_histoDataMgrSvc);
+//     m_histoPersSvc    = releaseInterface(m_histoPersSvc);
+//     return MinimalEventLoopMgr::terminate();
+//  }
+
+//=========================================================================
+// executeEvent(void* par)
+//=========================================================================
+StatusCode PileUpEventLoopMgr::executeEvent(void* par)    
+{
+  // Fire BeginEvent "Incident"
+  p_incidentSvc->fireIncident(Incident(name(),"BeginEvent"));
+  // Execute Algorithms
+  StatusCode sc = MinimalEventLoopMgr::executeEvent(par);
+  // Fire EndEvent "Incident"
+  p_incidentSvc->fireIncident(Incident(name(),"EndEvent"));
+
+  //------------------------------------------------------------------------
+  // Check if there was an error processing current event
+  //------------------------------------------------------------------------
+  if( !sc.isSuccess() )
+  {
+    MsgStream log( msgSvc(), name() );
+    log << MSG::ERROR 
+	<< "Terminating event processing loop due to errors" 
+	<< endreq;
+  }
+  return sc;
+}
+
+//=========================================================================
+// implementation of IAppMgrUI::nextEvent
+//=========================================================================
+StatusCode PileUpEventLoopMgr::nextEvent(int maxevt)   
+{
+  static int        total_nevt = 0;
+  MsgStream log( msgSvc(), name() );
+
+  if (!(setupStreams()).isSuccess())  return StatusCode::FAILURE;
+
+  // These following two initialization loops are performed here in case new
+  // Top level Algorithms or Output Streams have been created interactively
+  // at run-time instead of configuration time. Note also that it is possible
+  // that some newly created Algorithms are still not initialized as a result
+  // of these loops (e.g. where the membership of an existing Sequencer is 
+  // changed at run-time. In this case, the Algorithm::sysExecute() function 
+  // will ensure that the Algorithm is correctly initialized. This mechanism 
+  // actually makes loops redundant, but they do  provide a well defined 
+  // location for the initialization to take place in the non-interactive case.
+
+  // Initialize the list of Algorihms. Note that existing Algorithms
+  // are protected against multiple initialization attempts.
+  ListAlg::iterator ita;
+  for ( ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ++ita ) 
+  {
+    if( !((*ita)->sysInitialize()).isSuccess() ) 
+    {
+      log << MSG::ERROR 
+	  << "Unable to initialize Algorithm: " 
+	  << (*ita)->name() 
+	  << endreq;
+      return StatusCode::FAILURE;
+    }
+  }
+
+  // Initialize the list of Output Streams. Note that existing Output Streams
+  // are protected against multiple initialization attempts.
+  for (ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ++ita ) 
+  {
+    if( !((*ita)->sysInitialize()).isSuccess() ) {
+      log << MSG::ERROR 
+	  << "Unable to initialize Output Stream: " 
+	  << (*ita)->name() 
+	  << endreq;
+      return StatusCode::FAILURE;
+    }
+  }
+
+  // loop over events if the maxevt (received as input) is different from -1.
+  // if evtmax is -1 it means infinite loop
+  for( int nevt = 0; (maxevt == -1 ? true : nevt < maxevt);
+       ++nevt, ++total_nevt) 
+  {
+    //-----------------------------------------------------------------------
+    // Clear the event store, if used in the event loop
+    //-----------------------------------------------------------------------
+    if( 0 != total_nevt && 
+	!( (p_SGOver->clearStore()).isSuccess() &&  
+	   (p_SGOrig->clearStore()).isSuccess() ) )  
+      //FIXME clearbkg stores as well!!!
+    {
+      log << MSG::ERROR 
+	  << "Clear of Event data stores failed" << endreq;
+      return StatusCode::FAILURE;
+    }
+
+    //-----------------------------------------------------------------------
+    // Setup overlaid event in the event store
+    //-----------------------------------------------------------------------
+    EventInfo* pOverEvent = new EventInfo();
+    if( !(p_SGOver->record(pOverEvent)).isSuccess() )  {
+      log << MSG::WARNING 
+	  << "Error declaring event  data object" << endreq;
+      break;
+    } 
+
+    //-----------------------------------------------------------------------
+    // Read original event in the event store
+    //-----------------------------------------------------------------------
+    // Create root address and assign address to data service
+    IOpaqueAddress* oaddr = **p_origIter;
+
+    // Create an IOpaqueAddress for the Event Object:
+    if( !(p_SGOrig->createProxy(oaddr)).isSuccess() )  {
+      log << MSG::WARNING 
+	  << "Error declaring Event object" << endreq;
+      continue;
+    }
+    
+    // Retrieve the Event object
+    const EventInfo* pEvent;
+    if( !(p_SGOrig->retrieve(pEvent)).isSuccess() ) {
+      log << MSG::WARNING 
+	  << "Unable to retrieve Event root object" << endreq;
+      break;
+    }
+
+    //-----------------------------------------------------------------------
+    // Read input events in the bkg stores
+    //-----------------------------------------------------------------------
+    unsigned int i(0);
+    for(;i<m_nStores; i++) 
+    {
+      //check input selector is not empty
+      if (0 == m_bkgIterVec[i] ||
+	  *(m_bkgIterVec[i]) == *((m_bkgSelVec[i])->end()) ) 
+	{
+	  // This is the end of the loop. No more events in the selection
+	  log << MSG::ERROR
+	      << "Backgorund selector " << i
+	      << "has no more events" << endreq;
+	  break;
+	}
+
+      // Create root address and assign address to data service
+      IOpaqueAddress* addr = **(m_bkgIterVec[i]);
+
+      //FIXME ohmygosh
+      if (0 == m_bkgSGSvcVec[i]->locatePersistent(addr->clID(), 
+						  addr->containerName()) &&
+	  !(m_bkgSGSvcVec[i]->createProxy(addr)).isSuccess() )
+      {
+	log << MSG::ERROR 
+	    << "Error declaring Event object" 
+	    << " for input selector " << i << endreq;
+	break;
+      }
+      // Create an IOpaqueAddress for the Event Object:
+
+
+      // Retrieve the Event object
+      const EventInfo* pInputEvent;
+      if( !(m_bkgSGSvcVec[i]->retrieve(pInputEvent)).isSuccess() ) {
+	log << MSG::WARNING 
+	    << "Unable to retrieve Event root object" << endreq;
+	break;
+      }
+      //FIXME pOverEvent->addLink(pInputEvent);
+      // increment event iterators
+      if (0 != p_origIter) ++(*p_origIter);
+      if (0 != m_bkgIterVec[i])  ++(*(m_bkgIterVec[i]));
+    }
+    if (i<m_nStores) break; //one of the inputs is at its end. We are done!
+
+    
+
+    // Execute event for all required algorithms
+    if( !(executeEvent(NULL).isSuccess()) )
+    {
+      log << MSG::ERROR 
+	  << "Terminating event processing loop due to errors" << endreq;
+      break;
+    }
+
+  }
+
+  return StatusCode::SUCCESS;
+
+}
+
+
+
+
+
+//=========================================================================
+// setup input and overlay selctors and iterators
+//=========================================================================
+StatusCode
+PileUpEventLoopMgr::setupStreams()   
+{
+  StatusCode sc(StatusCode::SUCCESS);
+  MsgStream log( msgSvc(), name() );
+
+  const std::string& origSelName = m_origSelName.value();
+  const std::vector<std::string>& selecNames = m_selecNames.value();
+  //have we added any new selectors since last time we run this?
+  if (0 != m_nInputs && m_nInputs == selecNames.size()) return sc;
+  
+  //is there any bkg selector name?
+  if ((m_nInputs = selecNames.size()) == 0) {
+    //no bkg selector yet     
+    // We do not expect an Event Selector necessarily being declared
+    // so this is not a failure 
+    log << MSG::WARNING << "No Bkg EventSelector defined." << endreq;
+  }
+
+  //OK now we can run
+  // retrieve selector and setup iterator (the selector has been setup as an ExtService)
+
+  if  (!(serviceLocator()->service(origSelName, p_origSel )).isSuccess()   ||
+       ( 0 == p_origSel ) ) {
+    log << MSG::FATAL 
+	<< "Failed to find selector for " << origSelName 
+	<< endreq;
+    return StatusCode::FAILURE;
+  }
+  p_origIter = p_origSel->begin();
+
+  if (! (setupStore("OriginalEvent", p_SGOrig)).isSuccess())
+    return StatusCode::FAILURE;
+
+
+  //get bkg selectors, stores and iters
+  if (0 == m_nInputs) return sc;
+  m_nStores = (int)(2.0*m_collXing);     //FIXME this is too static
+  m_bkgSelVec.reserve(m_nStores);
+  m_bkgIterVec.reserve(m_nStores);  
+  m_bkgSGSvcVec.reserve(m_nStores);
+
+  for (unsigned int i=0; i < m_nStores; i++) {
+    //FIXME associate selectors to streams in a round robin fashion
+    //FIXME should use m_StreamSelectors
+    //FIXME here we are assuming all selectors have default config
+    const std::string& selecName(selecNames[i % m_nInputs]);
+
+    //  FIXME old gcc
+    ostrstream bufName;
+    bufName << '_' << i <<'\0'; 
+    const std::string& copyName(bufName.str());
+
+    IEvtSelector* pSel(0);
+    StoreGateSvc* pSG(0);
+    if (!(setupSelector(selecName, selecName + copyName, pSel)).isSuccess() ||
+	!(setupStore(std::string("BkgEvent") + copyName, pSG)).isSuccess())
+      return StatusCode::FAILURE;    
+
+    m_bkgSelVec.push_back(pSel); 
+    //init bkg iters at selector begin()
+    // It does not matter as the bkg selectors return a random event anyway
+    m_bkgIterVec.push_back(pSel->begin());
+    m_bkgSGSvcVec.push_back(pSG);
+  } 
+  
+  return sc;
+}
+
+
+StatusCode PileUpEventLoopMgr::setupSelector(const std::string& selecName,
+					     const std::string& copyName,
+					     IEvtSelector*& pSel) {
+  MsgStream log( msgSvc(), name() );
+
+  Service *parent, *child;
+  //if the selector is not there and can't be created barf
+  //remember the clone function also initializes the service if needed
+  IService* pIS(0);
+  if ( serviceLocator()->getService(selecName, pIS).isSuccess() &&
+       0 != (parent = dynamic_cast<Service*>(pIS))  &&
+       CloneService::clone(parent, copyName, child).isSuccess() &&
+       0 != (pSel = dynamic_cast<IEvtSelector*>(child)) )
+    return StatusCode::SUCCESS;
+  else 
+    return StatusCode::FAILURE;
+}
+
+StatusCode PileUpEventLoopMgr::setupStore(const std::string& storeName,
+					  StoreGateSvc*& pSG) {
+  MsgStream log( msgSvc(), name() );
+
+  if (service(storeName, pSG).isSuccess()) {
+    log << MSG::DEBUG 
+	<< "Found store " << storeName 
+	<< endreq;
+    return StatusCode::SUCCESS;
+  }
+
+  Service *parent, *child;
+  //if the selector is not there and can't be created barf
+  //remember the clone function also initializes the service if needed
+  IService* pIS(0);
+  if ( serviceLocator()->getService("StoreGateSvc", pIS).isSuccess() &&
+       0 != (parent = dynamic_cast<Service*>(pIS))  &&
+       CloneService::clone(parent, storeName, child).isSuccess() &&
+       0 != (pSG = dynamic_cast<StoreGateSvc*>(child)) ) {
+    log << MSG::DEBUG 
+	<< "Created store " << storeName 
+	<< endreq;
+    return StatusCode::SUCCESS;
+  }  else { 
+    log << MSG::ERROR
+	<< "Failed to create store " << storeName
+	<< endreq;
+    return StatusCode::FAILURE;
+  }
+}
+
+
+
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.