[Rivet-svn] r2629 - in trunk: include/Rivet pyext/rivet src/Core

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Fri Jul 30 19:52:48 BST 2010


Author: buckley
Date: Fri Jul 30 19:52:47 2010
New Revision: 2629

Log:
Rewriting AnalysisHandler to use shared pointers for Analysis objects and the LWH analysis factory (all other LWH factories are deleted by this one)

Modified:
   trunk/include/Rivet/AnalysisHandler.hh
   trunk/pyext/rivet/rivetwrap.i
   trunk/src/Core/AnalysisHandler.cc
   trunk/src/Core/AnalysisInfo.cc

Modified: trunk/include/Rivet/AnalysisHandler.hh
==============================================================================
--- trunk/include/Rivet/AnalysisHandler.hh	Fri Jul 30 12:27:58 2010	(r2628)
+++ trunk/include/Rivet/AnalysisHandler.hh	Fri Jul 30 19:52:47 2010	(r2629)
@@ -3,6 +3,7 @@
 #define RIVET_RivetHandler_HH
 
 #include "Rivet/Rivet.hh"
+#include "Rivet/RivetBoost.hh"
 #include "Rivet/Tools/Logging.fhh"
 #include "Rivet/AnalysisHandler.fhh"
 #include "Rivet/Analysis.fhh"
@@ -11,6 +12,16 @@
 
 namespace Rivet {
 
+  /// Typedef for Analysis (smart) pointer
+  typedef shared_ptr<Analysis> AnaHandle;
+
+  // Needed to make smart pointers compare equivalent in the STL set
+  struct AnaHandleLess {
+    bool operator()(const AnaHandle& a, const AnaHandle& b) {
+      return a.get() < b.get();
+    }
+  };
+
 
   /// A class which handles a number of analysis objects to be applied to
   /// generated events. An {@link Analysis}' AnalysisHandler is also responsible
@@ -158,8 +169,8 @@
     /// Get a list of the currently registered analyses' names.
     std::vector<std::string> analysisNames() const;
 
-    /// Get a list of the currently registered analyses' names.
-    const std::set<Analysis*>& analyses() const {
+    /// Get the collection of currently registered analyses.
+    const std::set<AnaHandle, AnaHandleLess>& analyses() const {
       return _analyses;
     }
 
@@ -258,7 +269,7 @@
   private:
 
     /// The collection of Analysis objects to be used.
-    set<Analysis*> _analyses;
+    set<AnaHandle, AnaHandleLess> _analyses;
 
 
     /// @name Run properties
@@ -286,10 +297,14 @@
 
 
     /// @name AIDA factory handles
+    /// Note that only the analysis factory can be a shared_ptr, since it deletes all the others.
     //@{
 
     /// The AIDA analysis factory.
-    AIDA::IAnalysisFactory* _theAnalysisFactory;
+    shared_ptr<AIDA::IAnalysisFactory> _theAnalysisFactory;
+
+    /// The AIDA tree factory.
+    AIDA::ITreeFactory* _theTreeFactory;
 
     /// The AIDA tree object.
     AIDA::ITree* _theTree;

Modified: trunk/pyext/rivet/rivetwrap.i
==============================================================================
--- trunk/pyext/rivet/rivetwrap.i	Fri Jul 30 12:27:58 2010	(r2628)
+++ trunk/pyext/rivet/rivetwrap.i	Fri Jul 30 19:52:47 2010	(r2629)
@@ -118,9 +118,6 @@
   class AnalysisHandler {
   public:
     AnalysisHandler(const std::string& runname);
-    AnalysisHandler(const std::string& basefilename,
-                    const std::string& runname,
-                    HistoFormat storetype=AIDAML);
     std::string runName() const;
     size_t numEvents() const;
     double sumOfWeights() const;

Modified: trunk/src/Core/AnalysisHandler.cc
==============================================================================
--- trunk/src/Core/AnalysisHandler.cc	Fri Jul 30 12:27:58 2010	(r2628)
+++ trunk/src/Core/AnalysisHandler.cc	Fri Jul 30 19:52:47 2010	(r2629)
@@ -19,7 +19,7 @@
       _sumOfWeights(0.0), _xs(-1.0),
       _initialised(false)
   {
-    _theAnalysisFactory = createAnalysisFactory();
+    _theAnalysisFactory.reset( createAnalysisFactory() );
     _setupFactories();
   }
 
@@ -32,7 +32,7 @@
   {
     cerr << "AnalysisHandler(basefilename, runname, format) constructor is deprecated: "
          << "please migrate your code to use the one-arg constructor" << endl;
-    _theAnalysisFactory = createAnalysisFactory();
+    _theAnalysisFactory.reset( createAnalysisFactory() );
     _setupFactories(basefilename, storetype);
   }
 
@@ -67,7 +67,7 @@
     // Check that analyses are beam-compatible
     const size_t num_anas_requested = analysisNames().size();
     removeIncompatibleAnalyses(beamIds());
-    foreach (const Analysis* a, analyses()) {
+    foreach (const AnaHandle a, analyses()) {
       if (toUpper(a->status()) != "VALIDATED") {
         getLog() << Log::WARN
                  << "Analysis '" << a->name() << "' is unvalidated: be careful!" << endl;
@@ -80,7 +80,7 @@
       exit(1);
     }
 
-    foreach (Analysis* a, _analyses) {
+    foreach (AnaHandle a, _analyses) {
       getLog() << Log::DEBUG << "Initialising analysis: " << a->name() << endl;
       // Allow projection registration in the init phase onwards
       a->_allowProjReg = true;
@@ -124,7 +124,7 @@
       setCrossSection(xs);
     }
     #endif
-    foreach (Analysis* a, _analyses) {
+    foreach (AnaHandle a, _analyses) {
       //getLog() << Log::DEBUG << "About to run analysis " << a->name() << endl;
       a->analyze(event);
       //getLog() << Log::DEBUG << "Finished running analysis " << a->name() << endl;
@@ -135,7 +135,7 @@
   void AnalysisHandler::finalize() {
     assert(_initialised);
     getLog() << Log::INFO << "Finalising analyses" << endl;
-    foreach (Analysis* a, _analyses) {
+    foreach (AnaHandle a, _analyses) {
       a->finalize();
     }
 
@@ -149,15 +149,8 @@
 
     // Delete analyses
     getLog() << Log::DEBUG << "Deleting analyses" << endl;
-    foreach (Analysis* a, _analyses) {
-      delete a;
-    }
     _analyses.clear();
 
-    // Delete singletons
-    //ProjectionHandler::destroy();
-    //HistoHandler::destroy();
-
     // Print out MCnet boilerplate
     cout << endl;
     cout << "The MCnet usage guidelines apply to Rivet: see http://www.montecarlonet.org/GUIDELINES" << endl;
@@ -167,15 +160,16 @@
 
   AnalysisHandler& AnalysisHandler::addAnalysis(const string& analysisname) {
     // Check for a duplicate analysis
-    /// @todo Might we want to be able to run an analysis twice, with different params? Requires avoiding histo clashes.
-    foreach (const Analysis* a, _analyses) {
+    /// @todo Might we want to be able to run an analysis twice, with different params?
+    ///       Requires avoiding histo tree clashes, i.e. storing the histos on the analysis objects.
+    foreach (const AnaHandle& a, _analyses) {
       if (a->name() == analysisname) {
         getLog() << Log::WARNING << "Analysis '" << analysisname << "' already registered: skipping duplicate" << endl;
         return *this;
       }
     }
-    Analysis* analysis = AnalysisLoader::getAnalysis(analysisname);
-    if (analysis) { // < Check for null analysis.
+    AnaHandle analysis( AnalysisLoader::getAnalysis(analysisname) );
+    if (analysis.get() != 0) { // < Check for null analysis.
       getLog() << Log::DEBUG << "Adding analysis '" << analysisname << "'" << endl;
       analysis->_analysishandler = this;
       _analyses.insert(analysis);
@@ -185,34 +179,32 @@
 
 
   AnalysisHandler& AnalysisHandler::removeAnalysis(const string& analysisname) {
-    Analysis* toremove = 0;
-    foreach (Analysis* a, _analyses) {
+    shared_ptr<Analysis> toremove;
+    foreach (const AnaHandle a, _analyses) {
       if (a->name() == analysisname) {
-        toremove = a;
+        toremove.reset( a.get() );
         break;
       }
     }
-    if (toremove) {
+    if (toremove.get() != 0) {
       getLog() << Log::DEBUG << "Removing analysis '" << analysisname << "'" << endl;
       _analyses.erase(toremove);
-      delete toremove;
     }
     return *this;
   }
 
 
   AnalysisHandler& AnalysisHandler::removeIncompatibleAnalyses(const PdgIdPair& beams) {
-    vector<Analysis*> todelete;
-    foreach (Analysis* a, _analyses) {
+    vector<string> anamestodelete;
+    foreach (const AnaHandle a, _analyses) {
       if (! a->isCompatible(beams)) {
-        todelete.push_back(a);
+        anamestodelete.push_back(a->name());
       }
     }
-    foreach (Analysis* a, todelete) {
+    foreach (const string& aname, anamestodelete) {
       getLog() << Log::WARN << "Removing incompatible analysis '"
-               << a->name() << "'" << endl;
-      _analyses.erase(a);
-      delete a;
+               << aname << "'" << endl;
+      removeAnalysis(aname);
     }
     return *this;
   }
@@ -230,18 +222,16 @@
       if (!endsWith(filename, ".root")) filename += ".root";
       storetypestr = "root";
     }
-    AIDA::ITreeFactory* tf = _theAnalysisFactory->createTreeFactory();
-    _theTree = tf->create(filename, storetypestr, false, true);
-    delete tf;
+    _theTreeFactory = _theAnalysisFactory->createTreeFactory();
+    _theTree = _theTreeFactory->create(filename, storetypestr, false, true);
     _theHistogramFactory = _theAnalysisFactory->createHistogramFactory(tree());
     _theDataPointSetFactory = _theAnalysisFactory->createDataPointSetFactory(tree());
   }
 
 
   void AnalysisHandler::_setupFactories() {
-    AIDA::ITreeFactory* tf = _theAnalysisFactory->createTreeFactory();
-    _theTree = tf->create();
-    delete tf;
+    _theTreeFactory = _theAnalysisFactory->createTreeFactory();
+    _theTree = _theTreeFactory->create();
     _theHistogramFactory = _theAnalysisFactory->createHistogramFactory(tree());
     _theDataPointSetFactory = _theAnalysisFactory->createDataPointSetFactory(tree());
   }
@@ -314,6 +304,7 @@
   size_t AnalysisHandler::numEvents() const { return _numEvents; }
   double AnalysisHandler::sumOfWeights() const { return _sumOfWeights; }
 
+
   void AnalysisHandler::setSumOfWeights(const double& sum) {
     _sumOfWeights=sum;
   }
@@ -321,7 +312,7 @@
 
   std::vector<std::string> AnalysisHandler::analysisNames() const {
     std::vector<std::string> rtn;
-    foreach (Analysis* a, _analyses) {
+    foreach (AnaHandle a, _analyses) {
       rtn.push_back(a->name());
     }
     return rtn;
@@ -368,7 +359,7 @@
 
   bool AnalysisHandler::needCrossSection() const {
     bool rtn = false;
-    foreach (const Analysis* a, _analyses) {
+    foreach (const AnaHandle a, _analyses) {
       if (!rtn) rtn = a->needsCrossSection();
       if (rtn) break;
     }
@@ -378,7 +369,7 @@
 
   AnalysisHandler& AnalysisHandler::setCrossSection(double xs) {
     _xs = xs;
-    foreach (Analysis* a, _analyses) {
+    foreach (AnaHandle a, _analyses) {
       a->setCrossSection(xs);
     }
     return *this;
@@ -389,16 +380,19 @@
     return (crossSection() >= 0);
   }
 
+
   AnalysisHandler& AnalysisHandler::addAnalysis(Analysis* analysis) {
     analysis->_analysishandler = this;
-    _analyses.insert(analysis);
+    _analyses.insert(AnaHandle(analysis));
     return *this;
   }
 
+
   PdgIdPair AnalysisHandler::beamIds() const {
     return Rivet::beamIds(beams());
   }
 
+
   double AnalysisHandler::sqrtS() const {
     return Rivet::sqrtS(beams());
   }

Modified: trunk/src/Core/AnalysisInfo.cc
==============================================================================
--- trunk/src/Core/AnalysisInfo.cc	Fri Jul 30 12:27:58 2010	(r2628)
+++ trunk/src/Core/AnalysisInfo.cc	Fri Jul 30 19:52:47 2010	(r2629)
@@ -188,7 +188,7 @@
           << key << "' from " << datapath << endl;
       }
     }
-    Log::getLog("Rivet.AnalysisInfo") << Log::DEBUG << ai << endl;
+    Log::getLog("Rivet.AnalysisInfo") << Log::TRACE << "AnalysisInfo pointer = " << ai << endl;
     return ai;
   }
 


More information about the Rivet-svn mailing list