rivet is hosted by Hepforge, IPPP Durham
AnalysisHandler.hh
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 #ifndef RIVET_RivetHandler_HH
00003 #define RIVET_RivetHandler_HH
00004 
00005 #include "Rivet/Config/RivetCommon.hh"
00006 #include "Rivet/Particle.hh"
00007 #include "Rivet/AnalysisLoader.hh"
00008 #include "Rivet/Tools/RivetYODA.hh"
00009 
00010 namespace Rivet {
00011 
00012 
00013   // Forward declaration and smart pointer for Analysis
00014   class Analysis;
00015   typedef shared_ptr<Analysis> AnaHandle;
00016 
00017 
00018   // Needed to make smart pointers compare equivalent in the STL set
00019   struct CmpAnaHandle {
00020     bool operator() (const AnaHandle& a, const AnaHandle& b) {
00021       return a.get() < b.get();
00022     }
00023   };
00024 
00025 
00026   /// A class which handles a number of analysis objects to be applied to
00027   /// generated events. An {@link Analysis}' AnalysisHandler is also responsible
00028   /// for handling the final writing-out of histograms.
00029   class AnalysisHandler {
00030   public:
00031 
00032     /// @name Constructors and destructors. */
00033     //@{
00034 
00035     /// Preferred constructor, with optional run name.
00036     AnalysisHandler(const string& runname="");
00037 
00038     /// @brief Destructor
00039     /// The destructor is not virtual, as this class should not be inherited from.
00040     ~AnalysisHandler();
00041 
00042     //@}
00043 
00044 
00045   private:
00046 
00047     /// Get a logger object.
00048     Log& getLog() const;
00049 
00050 
00051   public:
00052 
00053     /// @name Run properties
00054     //@{
00055 
00056     /// Get the name of this run.
00057     string runName() const;
00058 
00059 
00060     /// Get the number of events seen. Should only really be used by external
00061     /// steering code or analyses in the finalize phase.
00062     size_t numEvents() const;
00063 
00064     /// Get the sum of the event weights seen - the weighted equivalent of the
00065     /// number of events. Should only really be used by external steering code
00066     /// or analyses in the finalize phase.
00067     double sumOfWeights() const;
00068 
00069     /// Set sum of weights. This is useful if Rivet is steered externally and
00070     /// the analyses are run for a sub-contribution of the events
00071     /// (but of course have to be normalised to the total sum of weights)
00072     void setSumOfWeights(const double& sum);
00073 
00074 
00075     /// Is cross-section information required by at least one child analysis?
00076     bool needCrossSection() const;
00077 
00078     /// Set the cross-section for the process being generated.
00079     AnalysisHandler& setCrossSection(double xs);
00080 
00081     /// Get the cross-section known to the handler.
00082     double crossSection() const {
00083       return _xs;
00084     }
00085 
00086     /// Whether the handler knows about a cross-section.
00087     bool hasCrossSection() const;
00088 
00089 
00090     /// Set beams for this run
00091     AnalysisHandler& setRunBeams(const ParticlePair& beams) {
00092       _beams = beams;
00093       MSG_DEBUG("Setting run beams = " << beams << " @ " << sqrtS()/GeV << " GeV");
00094       return *this;
00095     }
00096 
00097     /// Get beam IDs for this run, usually determined from the first event.
00098     const ParticlePair& beams() const {
00099       return _beams;
00100     }
00101 
00102     /// Get beam IDs for this run, usually determined from the first event.
00103     PdgIdPair beamIds() const;
00104 
00105     /// Get energy for this run, usually determined from the first event.
00106     double sqrtS() const;
00107 
00108     /// Setter for _ignoreBeams
00109     void setIgnoreBeams(bool ignore=true);
00110 
00111     //@}
00112 
00113 
00114     /// @name Handle analyses
00115     //@{
00116 
00117     /// Get a list of the currently registered analyses' names.
00118     std::vector<std::string> analysisNames() const;
00119 
00120     /// Get the collection of currently registered analyses.
00121     const std::set<AnaHandle, CmpAnaHandle>& analyses() const {
00122       return _analyses;
00123     }
00124 
00125     /// Add an analysis to the run list using its name. The actual Analysis
00126     /// to be used will be obtained via AnalysisHandler::getAnalysis(string).
00127     /// If no matching analysis is found, no analysis is added (i.e. the
00128     /// null pointer is checked and discarded.
00129     AnalysisHandler& addAnalysis(const std::string& analysisname);
00130 
00131     /// Remove an analysis from the run list using its name.
00132     AnalysisHandler& removeAnalysis(const std::string& analysisname);
00133 
00134 
00135     /// Add analyses to the run list using their names. The actual {@link
00136     /// Analysis}' to be used will be obtained via
00137     /// AnalysisHandler::addAnalysis(string), which in turn uses
00138     /// AnalysisHandler::getAnalysis(string). If no matching analysis is found
00139     /// for a given name, no analysis is added, but also no error is thrown.
00140     AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
00141 
00142     /// Remove analyses from the run list using their names.
00143     AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
00144 
00145     /// Add an analysis to the run list by object
00146     AnalysisHandler& addAnalysis(Analysis* analysis);
00147 
00148     //@}
00149 
00150 
00151     /// @name Main init/execute/finalise
00152     //@{
00153 
00154     /// Initialize a run, with the run beams taken from the example event.
00155     void init(const GenEvent& event);
00156 
00157     /// Analyze the given \a event. This function will call the
00158     /// AnalysisBase::analyze() function of all included analysis objects.
00159     void analyze(const GenEvent& event);
00160 
00161     /// Finalize a run. This function calls the AnalysisBase::finalize()
00162     /// functions of all included analysis objects.
00163     void finalize();
00164 
00165     //@}
00166 
00167 
00168     /// @name Histogram / data object access
00169     //@{
00170 
00171     /// Get all analyses' plots as a vector of analysis objects.
00172     std::vector<AnalysisObjectPtr> getData() const;
00173 
00174     /// Write all analyses' plots to the named file.
00175     void writeData(const std::string& filename) const;
00176 
00177     //@}
00178 
00179 
00180   private:
00181 
00182     /// The collection of Analysis objects to be used.
00183     set<AnaHandle, CmpAnaHandle> _analyses;
00184 
00185 
00186     /// @name Run properties
00187     //@{
00188 
00189     /// Run name
00190     std::string _runname;
00191 
00192     /// Number of events seen.
00193     /// @todo Replace by a counter
00194     unsigned int _numEvents;
00195     /// Sum of event weights seen.
00196     /// @todo Replace by a counter
00197     double _sumOfWeights, _sumOfWeightsSq;
00198 
00199     /// Cross-section known to AH
00200     double _xs, _xserr;
00201 
00202     /// Beams used by this run.
00203     ParticlePair _beams;
00204 
00205     /// Flag to check if init has been called
00206     bool _initialised;
00207 
00208     /// Flag whether input event beams should be ignored in compatibility check
00209     bool _ignoreBeams;
00210 
00211     //@}
00212 
00213   private:
00214 
00215     /// The assignment operator is private and must never be called.
00216     /// In fact, it should not even be implemented.
00217     AnalysisHandler& operator=(const AnalysisHandler&);
00218 
00219     /// The copy constructor is private and must never be called.  In
00220     /// fact, it should not even be implemented.
00221     AnalysisHandler(const AnalysisHandler&);
00222 
00223   };
00224 
00225 
00226 }
00227 
00228 #endif