[Rivet-svn] r1945 - in trunk: bin include/Rivet src

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Wed Oct 21 10:56:52 BST 2009


Author: fsiegert
Date: Wed Oct 21 10:56:52 2009
New Revision: 1945

Log:
Make signal handling work again by moving the event loop back into
Python, albeit without touching HepMC.

Modified:
   trunk/bin/rivet
   trunk/include/Rivet/Run.hh
   trunk/src/Run.cc

Modified: trunk/bin/rivet
==============================================================================
--- trunk/bin/rivet	Wed Oct 21 10:40:59 2009	(r1944)
+++ trunk/bin/rivet	Wed Oct 21 10:56:52 2009	(r1945)
@@ -332,15 +332,41 @@
 ah.init()
         
 
+## Event number logging
+def logNEvt(n, starttime, maxevtnum):
+    nevtloglevel = logging.DEBUG
+    if n % 10 == 0:
+        nevtloglevel = logging.DEBUG + 5
+    if n % 100 == 0:
+        nevtloglevel = logging.INFO
+    if n % 200 == 0:
+        nevtloglevel = logging.INFO + 5
+    if n % 500 == 0:
+        nevtloglevel = logging.WARNING
+    if n % 1000 == 0:
+        nevtloglevel = logging.WARNING + 5
+    if n % 10000 == 0:
+        nevtloglevel = logging.CRITICAL
+    timecurrent = time.time()
+    timeelapsed = timecurrent - starttime;
+    if maxevtnum is None:
+        logging.log(nevtloglevel, "Event %d (%d s elapsed)" % (n, timeelapsed))
+    else:
+        timeleft = (maxevtnum-n)*timeelapsed/n
+        eta = time.strftime("%a %b %d %H:%M", time.localtime(timecurrent + timeleft))
+        logging.log(nevtloglevel, "Event %d (%d s elapsed / %d s left) -> ETA: %s"
+                    % (n, timeelapsed, timeleft, eta))
+
+
 ## Read and process events
 run = rivet.Run(ah)
 if opts.CROSS_SECTION is not None:
     run.setCrossSection(opts.CROSS_SECTION)
-if opts.MAXEVTNUM is not None:
-    run.setMaxEvtNum(opts.MAXEVTNUM)
 if opts.LIST_USED_ANALYSES is not None:
     run.setListAnalyses(opts.LIST_USED_ANALYSES)
 
+EVTNUM = 0
+starttime = time.time()
 
 for evtfile in HEPMCFILES:
     import platform
@@ -348,11 +374,23 @@
     if opts.CROSS_SECTION is not None:
         logging.info("User-supplied cross-section = %e pb" % opts.CROSS_SECTION)
     logging.info("Reading events from '%s'" % evtfile)
-    if not run.processFile(evtfile):
+    if not run.prepareFile(evtfile):
         break
     
+    while opts.MAXEVTNUM is None or EVTNUM < opts.MAXEVTNUM:
+        EVTNUM += 1
+        logNEvt(EVTNUM, starttime, opts.MAXEVTNUM)
+        if not run.processEvent(EVTNUM==1):
+            break
+        if RECVD_KILL_SIGNAL is not None:
+            break
+        
+    if not run.finalizeFile():
+        break
+        
     if RECVD_KILL_SIGNAL is not None:
         break
+    
 logging.info("Finished event loop")
 
 ## Override any generator cross-section to that supplied on the command line, if there was one

Modified: trunk/include/Rivet/Run.hh
==============================================================================
--- trunk/include/Rivet/Run.hh	Wed Oct 21 10:40:59 2009	(r1944)
+++ trunk/include/Rivet/Run.hh	Wed Oct 21 10:56:52 2009	(r1945)
@@ -4,6 +4,10 @@
 
 #include "Rivet/AnalysisHandler.fhh"
 
+namespace HepMC {
+  class IO_GenEvent;
+}
+
 namespace Rivet {
 
 
@@ -25,24 +29,22 @@
 
     /// Get the name of this run.
     Run& setCrossSection(const double& xs);
-    Run& setMaxEvtNum(const int& n);
     Run& setListAnalyses(const bool& );
     
-    bool processFile(const std::string& evtfile);
+    bool prepareFile(const std::string& evtfile);
+    bool processEvent(bool firstEvent);
+    bool finalizeFile();
     
   private:
-    
-    void logNEvt();
-
-
-  private:
 
     /// AnalysisHandler object
     AnalysisHandler& _ah;
     
     double _xs;
-    long _maxEvtNum, _numEvents;
     bool _listAnalyses;
+    
+    HepMC::IO_GenEvent* m_io;
+    std::istream* m_istr;
 
   };
 

Modified: trunk/src/Run.cc
==============================================================================
--- trunk/src/Run.cc	Wed Oct 21 10:40:59 2009	(r1944)
+++ trunk/src/Run.cc	Wed Oct 21 10:56:52 2009	(r1945)
@@ -9,16 +9,11 @@
 
 
   Run::Run(AnalysisHandler& ah) : _ah(ah), _xs(-1.0),
-    _maxEvtNum(std::numeric_limits<long>::max()), _numEvents(0) {
+    m_io(NULL), m_istr(NULL) {
   }
   
   
   Run::~Run() {
-    if (_maxEvtNum != std::numeric_limits<long>::max() && _numEvents < _maxEvtNum) {
-      Log::getLog("Rivet.Run") << Log::WARN
-          << "Sampled fewer events (" << _numEvents << ") than expected "
-          << "(" << _maxEvtNum << ")" << endl;
-    }
   }
   
   
@@ -28,120 +23,103 @@
   }
   
   
-  Run& Run::setMaxEvtNum(const int& n) {
-    _maxEvtNum = n;
-    return *this;
-  }
-  
-  
   Run& Run::setListAnalyses(const bool& l) {
     _listAnalyses = l;
     return *this;
   }
   
   
-  bool Run::processFile(const std::string& evtfile) {
-    HepMC::IO_GenEvent* io = 0;
-    std::istream* istr = 0;
+  bool Run::prepareFile(const std::string& evtfile) {
     if (evtfile == "-") {
-      io = new HepMC::IO_GenEvent(std::cin);
+      m_io = new HepMC::IO_GenEvent(std::cin);
     } else {
       // Ignore the HepMC::IO_GenEvent(filename, ios) constructor, since only available from HepMC 2.4
-      istr = new std::fstream(evtfile.c_str(), std::ios::in);
-      io = new HepMC::IO_GenEvent(*istr);
+      m_istr = new std::fstream(evtfile.c_str(), std::ios::in);
+      m_io = new HepMC::IO_GenEvent(*m_istr);
     }
-    if (io->rdstate() != 0) {
+    if (m_io->rdstate() != 0) {
       Log::getLog("Rivet.Run") << Log::ERROR << "Read error on file " << evtfile << endl;
       return false;
     }
-    
+  
+    return true;
+  }
+  
+  bool Run::processEvent(bool firstEvent) {
     GenEvent* evt = new GenEvent();
-    while (io->fill_next_event(evt)) {
+    if (!m_io->fill_next_event(evt)) {
+      delete evt;
+      return false;
+    }
 
-      // Check for a bad read
-      if (io->rdstate() != 0) {
-        Log::getLog("Rivet.Run") << Log::DEBUG << "End of file?" << endl;
-        break;
+    // Check for a bad read
+    if (m_io->rdstate() != 0) {
+      Log::getLog("Rivet.Run") << Log::DEBUG << "End of file?" << endl;
+      delete evt;
+      return false;
+    }
+    
+    // Set up system based on properties of first event
+    if (firstEvent) {
+      // If empty
+      if (evt->particles_size() == 0) {
+        Log::getLog("Rivet.Run") << Log::ERROR << "Empty first event." << endl;
+        delete evt;
+        return false;
       }
-
-      // Set up system based on properties of first event
-      if (_numEvents == 0) {
-        // If empty
-        if (evt->particles_size() == 0) {
-          Log::getLog("Rivet.Run") << Log::ERROR << "Empty first event from " << evtfile << endl;
-          return false;
-        }
-        size_t num_anas_requested = _ah.analysisNames().size();
-        _ah.removeIncompatibleAnalyses(beamIds(*evt));
-        if (num_anas_requested > 0 && _ah.analysisNames().size() == 0) {
-          Log::getLog("Rivet.Run") << Log::ERROR
-              << "All analyses were incompatible with the first event's beams"
-              << "Exiting, since this probably isn't intentional!" << endl;
-          delete evt;
-          return false;
-        }
-        
-        if (_listAnalyses) {
-          foreach (const std::string& ana, _ah.analysisNames()) {
-            cout<<ana<<endl;
-          }
-        }
+      size_t num_anas_requested = _ah.analysisNames().size();
+      _ah.removeIncompatibleAnalyses(beamIds(*evt));
+      if (num_anas_requested > 0 && _ah.analysisNames().size() == 0) {
+        Log::getLog("Rivet.Run") << Log::ERROR
+            << "All analyses were incompatible with the first event's beams"
+            << "Exiting, since this probably isn't intentional!" << endl;
+        delete evt;
+        return false;
       }
       
-      if (_xs > 0.0) {
-        _ah.setCrossSection(_xs);
-      }
-      #ifdef HEPMC_HAS_CROSS_SECTION
-      else if (evt->cross_section()) {
-        /// @todo Use xs error?
-        const double xs = evt->cross_section()->cross_section(); //< in pb
-        Log::getLog("Rivet.Run") << Log::DEBUG
-                                 << "Setting cross-section = " << xs << " pb" << endl;
-        _ah.setCrossSection(xs);
-      }
-      #endif
-      else {
-        if (_ah.needCrossSection()) {
-          Log::getLog("Rivet.Run") << Log::ERROR
-              << "Total cross-section needed for at least one of the analyses. "
-              << "Please set it (on the command line with '-x' if using the 'rivet' program)" << endl;
-          return false;
+      if (_listAnalyses) {
+        foreach (const std::string& ana, _ah.analysisNames()) {
+          cout<<ana<<endl;
         }
       }
-      /// @todo If NOT first event, check that beams aren't changed
-      
-      // Analyze event and delete HepMC event object      
-      _ah.analyze(*evt);
-      delete evt;
-
-      // Increment, log, and check event number 
-      ++_numEvents;
-      logNEvt();
-      if (_numEvents == _maxEvtNum) return false;
-      
-      /// @todo Can we just clear the event, to save on all this mallocing?
-      evt = new GenEvent();
     }
-
-    // Final HepMC object clean-up
+    
+    if (_xs > 0.0) {
+      _ah.setCrossSection(_xs);
+    }
+#ifdef HEPMC_HAS_CROSS_SECTION
+    else if (evt->cross_section()) {
+      /// @todo Use xs error?
+      const double xs = evt->cross_section()->cross_section(); //< in pb
+      Log::getLog("Rivet.Run") << Log::DEBUG
+          << "Setting cross-section = " << xs << " pb" << endl;
+      _ah.setCrossSection(xs);
+    }
+#endif
+    else {
+      if (_ah.needCrossSection()) {
+        Log::getLog("Rivet.Run") << Log::ERROR
+            << "Total cross-section needed for at least one of the analyses. "
+            << "Please set it (on the command line with '-x' if using the 'rivet' program)" << endl;
+        delete evt;
+        return false;
+      }
+    }
+    /// @todo If NOT first event, check that beams aren't changed
+    
+    // Analyze event and delete HepMC event object      
+    _ah.analyze(*evt);
     delete evt;
-    delete io;
-    delete istr;
     
     return true;
   }
-  
-  
-  void Run::logNEvt() {
-    std::stringstream ss;
-    ss << "Event " << _numEvents;
-    const string msg = ss.str();
-    int lvl = Log::TRACE;
-    if (_numEvents % 10000 == 0) lvl = Log::ERROR;
-    else if (_numEvents % 1000 == 0) lvl = Log::WARN;
-    else if (_numEvents % 100 == 0) lvl = Log::INFO;
-    else if (_numEvents % 10 == 0) lvl = Log::DEBUG;
-    Log::getLog("Rivet.Run") << lvl << msg << endl;
+
+  bool Run::finalizeFile() {
+    // Final HepMC object clean-up
+    delete m_io;
+    if (m_istr) delete m_istr;
+    
+    return true;
   }
 
   


More information about the Rivet-svn mailing list