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/Rivet.hh"
00006 #include "Rivet/Tools/RivetBoost.hh"
00007 #include "Rivet/Tools/Logging.hh"
00008 #include "Rivet/AnalysisLoader.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 AnaHandleLess {
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, AnaHandleLess>& 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 
00146     /// Add an analysis to the run list by object
00147     AnalysisHandler& addAnalysis(Analysis* analysis);
00148 
00149     //@}
00150 
00151 
00152     /// @name Main init/execute/finalise
00153     //@{
00154 
00155     /// Initialize a run, with the run beams taken from the example event.
00156     void init(const GenEvent& event);
00157 
00158 
00159     /// Analyze the given \a event. This function will call the
00160     /// AnalysisBase::analyze() function of all included analysis objects.
00161     void analyze(const GenEvent& event);
00162 
00163 
00164     /// Finalize a run. This function calls the AnalysisBase::finalize()
00165     /// functions of all included analysis objects.
00166     void finalize();
00167 
00168     //@}
00169 
00170 
00171     /// Write all analyses' plots to the named file.
00172     void writeData(const std::string& filename);
00173 
00174 
00175   private:
00176 
00177     /// The collection of Analysis objects to be used.
00178     set<AnaHandle, AnaHandleLess> _analyses;
00179 
00180 
00181     /// @name Run properties
00182     //@{
00183 
00184     /// Run name
00185     std::string _runname;
00186 
00187     /// Number of events seen.
00188     unsigned int _numEvents;
00189 
00190     /// Sum of event weights seen.
00191     double _sumOfWeights;
00192 
00193     /// Cross-section known to AH
00194     double _xs;
00195 
00196     /// Beams used by this run.
00197     ParticlePair _beams;
00198 
00199     /// Flag to check if init has been called
00200     bool _initialised;
00201 
00202     /// Flag whether input event beams should be ignored in compatibility check
00203     bool _ignoreBeams;
00204 
00205     //@}
00206 
00207   private:
00208 
00209     /// The assignment operator is private and must never be called.
00210     /// In fact, it should not even be implemented.
00211     AnalysisHandler& operator=(const AnalysisHandler&);
00212 
00213     /// The copy constructor is private and must never be called.  In
00214     /// fact, it should not even be implemented.
00215     AnalysisHandler(const AnalysisHandler&);
00216 
00217   };
00218 
00219 
00220 }
00221 
00222 #endif