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/RivetBoost.hh"
00007 #include "Rivet/Tools/Logging.fhh"
00008 #include "Rivet/AnalysisHandler.fhh"
00009 #include "Rivet/Analysis.fhh"
00010 #include "Rivet/Event.fhh"
00011 #include "Rivet/AnalysisLoader.hh"
00012 
00013 namespace Rivet {
00014 
00015   /// Typedef for Analysis (smart) pointer
00016   typedef shared_ptr<Analysis> AnaHandle;
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 
00039     /// @brief Make a Rivet handler with a set base filename and store type.
00040     ///
00041     /// An AnalysisHandler built with this constructor sets the output histo format
00042     /// and filename when the handler is created rather than when it is written.
00043     /// This is not the preferred behaviour, to allow for more flexible histogramming
00044     /// in future, use the writeData() method to supply the filename and format at
00045     /// the point of file-writing.
00046     ///
00047     /// Note that the run name is now a compulsory argument: this is to avoid
00048     /// conflict with the preferred one-argument constructor.
00049     ///
00050     /// @deprecated Prefer to specify output files and formats explicitly.
00051     AnalysisHandler(const string& basefilename, const string& runname, HistoFormat storetype=AIDAML);
00052 
00053 
00054     /// @brief Destructor
00055     /// The destructor is not virtual, as this class should not be inherited from.
00056     ~AnalysisHandler();
00057 
00058     //@}
00059 
00060 
00061   private:
00062 
00063     /// Do the initialisation of the AIDA analysis factories.
00064     /// @deprecated When AIDA goes, this goes...
00065     void _setupFactories(const string& basefilename, HistoFormat storetype);
00066 
00067     /// Do the initialisation of the AIDA analysis factories with no store.
00068     /// @deprecated When AIDA goes, this goes...
00069     void _setupFactories();
00070 
00071     /// Convert any IHistogram1D objects in the AIDA tree to IDataPointSet objects.
00072     /// @deprecated When AIDA goes, this goes...
00073     void _normalizeTree(AIDA::ITree& tree);
00074 
00075     /// Get a logger object.
00076     Log& getLog() const;
00077 
00078 
00079   public:
00080 
00081     /// @name Run properties
00082     //@{
00083 
00084     /// Get the name of this run.
00085     string runName() const;
00086 
00087 
00088     /// Get the number of events seen. Should only really be used by external
00089     /// steering code or analyses in the finalize phase.
00090     size_t numEvents() const;
00091 
00092     /// Get the sum of the event weights seen - the weighted equivalent of the
00093     /// number of events. Should only really be used by external steering code
00094     /// or analyses in the finalize phase.
00095     double sumOfWeights() const;
00096 
00097     /// Set sum of weights. This is useful if Rivet is steered externally and
00098     /// the analyses are run for a sub-contribution of the events
00099     /// (but of course have to be normalised to the total sum of weights)
00100     void setSumOfWeights(const double& sum);
00101 
00102 
00103     /// Is cross-section information required by at least one child analysis?
00104     bool needCrossSection() const;
00105 
00106     /// Set the cross-section for the process being generated.
00107     AnalysisHandler& setCrossSection(double xs);
00108 
00109     /// Get the cross-section known to the handler.
00110     double crossSection() const {
00111       return _xs;
00112     }
00113 
00114     /// Whether the handler knows about a cross-section.
00115     bool hasCrossSection() const;
00116 
00117 
00118     /// Set beams for this run
00119     AnalysisHandler& setRunBeams(const ParticlePair& beams) {
00120       _beams = beams;
00121       MSG_DEBUG("Setting run beams = " << beams << " @ " << sqrtS()/GeV << " GeV");
00122       return *this;
00123     }
00124 
00125     /// Get beam IDs for this run, usually determined from the first event.
00126     const ParticlePair& beams() const {
00127       return _beams;
00128     }
00129 
00130     /// Get beam IDs for this run, usually determined from the first event.
00131     PdgIdPair beamIds() const;
00132 
00133     /// Get energy for this run, usually determined from the first event.
00134     double sqrtS() const;
00135 
00136     //@}
00137 
00138 
00139     /// @name Handle analyses
00140     //@{
00141 
00142     /// Get a list of the currently registered analyses' names.
00143     std::vector<std::string> analysisNames() const;
00144 
00145     /// Get the collection of currently registered analyses.
00146     const std::set<AnaHandle, AnaHandleLess>& analyses() const {
00147       return _analyses;
00148     }
00149 
00150     /// Add an analysis to the run list using its name. The actual Analysis
00151     /// to be used will be obtained via AnalysisHandler::getAnalysis(string).
00152     /// If no matching analysis is found, no analysis is added (i.e. the
00153     /// null pointer is checked and discarded.
00154     AnalysisHandler& addAnalysis(const std::string& analysisname);
00155 
00156     /// Remove an analysis from the run list using its name.
00157     AnalysisHandler& removeAnalysis(const std::string& analysisname);
00158 
00159 
00160     /// Add analyses to the run list using their names. The actual {@link
00161     /// Analysis}' to be used will be obtained via
00162     /// AnalysisHandler::addAnalysis(string), which in turn uses
00163     /// AnalysisHandler::getAnalysis(string). If no matching analysis is found
00164     /// for a given name, no analysis is added, but also no error is thrown.
00165     AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
00166 
00167     /// Remove analyses from the run list using their names.
00168     AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
00169 
00170 
00171     /// Add an analysis to the run list by object
00172     AnalysisHandler& addAnalysis(Analysis* analysis);
00173 
00174     //@}
00175 
00176 
00177     /// @name Main init/execute/finalise
00178     //@{
00179 
00180     /// @deprecated Obsolete method, kept only for backwards compatibility
00181     void init() {}
00182 
00183 
00184     /// Initialize a run, with the run beams taken from the example event.
00185     void init(const GenEvent& event);
00186 
00187 
00188     /// Analyze the given \a event. This function will call the
00189     /// AnalysisBase::analyze() function of all included analysis objects.
00190     void analyze(const GenEvent& event);
00191 
00192 
00193     /// Finalize a run. This function first calls the AnalysisBase::finalize()
00194     /// functions of all included analysis objects and converts all histograms
00195     /// to AIDA DataPointSet objects in the AIDA tree. Using the histogram tree
00196     /// for further analysis or writing to file is left to the API user.
00197     void finalize();
00198 
00199     //@}
00200 
00201 
00202     /// @name AIDA factories etc.
00203     /// @deprecated All this will be removed when histogramming is overhauled
00204     //@{
00205 
00206     /// The AIDA analysis factory.
00207     /// @deprecated When AIDA goes, this goes...
00208     AIDA::IAnalysisFactory& analysisFactory();
00209 
00210 
00211     /// Commit the AIDA tree to file.
00212     /// @deprecated When AIDA goes, this goes...
00213     void commitData();
00214 
00215 
00216     /// Write the AIDA tree to the named file.
00217     /// @deprecated When AIDA goes, this goes...
00218     void writeData(const std::string& filename);
00219 
00220 
00221     /// The AIDA tree object.
00222     /// @deprecated When AIDA goes, this goes...
00223     AIDA::ITree& tree();
00224 
00225 
00226     /// The AIDA histogram factory.
00227     /// @deprecated When AIDA goes, this goes...
00228     AIDA::IHistogramFactory& histogramFactory();
00229 
00230 
00231     /// The AIDA histogram factory.
00232     /// @deprecated When AIDA goes, this goes...
00233     AIDA::IDataPointSetFactory& datapointsetFactory();
00234 
00235     //@}
00236 
00237 
00238   private:
00239 
00240     /// The collection of Analysis objects to be used.
00241     set<AnaHandle, AnaHandleLess> _analyses;
00242 
00243 
00244     /// @name Run properties
00245     //@{
00246 
00247     /// Run name
00248     std::string _runname;
00249 
00250     /// Number of events seen.
00251     size_t _numEvents;
00252 
00253     /// Sum of event weights seen.
00254     double _sumOfWeights;
00255 
00256     /// Cross-section known to AH
00257     double _xs;
00258 
00259     /// Beams used by this run.
00260     ParticlePair _beams;
00261 
00262     /// Flag to check if init has been called
00263     bool _initialised;
00264 
00265     //@}
00266 
00267 
00268     /// @name AIDA factory handles
00269     /// Note that only the analysis factory can be a shared_ptr, since it deletes all the others.
00270     //@{
00271 
00272     /// The AIDA analysis factory.
00273     shared_ptr<AIDA::IAnalysisFactory> _theAnalysisFactory;
00274 
00275     /// The AIDA tree factory.
00276     AIDA::ITreeFactory* _theTreeFactory;
00277 
00278     /// The AIDA tree object.
00279     AIDA::ITree* _theTree;
00280 
00281     /// The AIDA histogram factory.
00282     AIDA::IHistogramFactory* _theHistogramFactory;
00283 
00284     /// The AIDA data point set factory.
00285     AIDA::IDataPointSetFactory* _theDataPointSetFactory;
00286 
00287     //@}
00288 
00289 
00290   private:
00291 
00292     /// The assignment operator is private and must never be called.
00293     /// In fact, it should not even be implemented.
00294     AnalysisHandler& operator=(const AnalysisHandler&);
00295 
00296     /// The copy constructor is private and must never be called.  In
00297     /// fact, it should not even be implemented.
00298     AnalysisHandler(const AnalysisHandler&);
00299 
00300   };
00301 
00302 
00303 }
00304 
00305 #endif