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     /// Setter for _ignoreBeams
00137     void setIgnoreBeams(bool ignore=true);
00138 
00139     //@}
00140 
00141 
00142     /// @name Handle analyses
00143     //@{
00144 
00145     /// Get a list of the currently registered analyses' names.
00146     std::vector<std::string> analysisNames() const;
00147 
00148     /// Get the collection of currently registered analyses.
00149     const std::set<AnaHandle, AnaHandleLess>& analyses() const {
00150       return _analyses;
00151     }
00152 
00153     /// Add an analysis to the run list using its name. The actual Analysis
00154     /// to be used will be obtained via AnalysisHandler::getAnalysis(string).
00155     /// If no matching analysis is found, no analysis is added (i.e. the
00156     /// null pointer is checked and discarded.
00157     AnalysisHandler& addAnalysis(const std::string& analysisname);
00158 
00159     /// Remove an analysis from the run list using its name.
00160     AnalysisHandler& removeAnalysis(const std::string& analysisname);
00161 
00162 
00163     /// Add analyses to the run list using their names. The actual {@link
00164     /// Analysis}' to be used will be obtained via
00165     /// AnalysisHandler::addAnalysis(string), which in turn uses
00166     /// AnalysisHandler::getAnalysis(string). If no matching analysis is found
00167     /// for a given name, no analysis is added, but also no error is thrown.
00168     AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
00169 
00170     /// Remove analyses from the run list using their names.
00171     AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
00172 
00173 
00174     /// Add an analysis to the run list by object
00175     AnalysisHandler& addAnalysis(Analysis* analysis);
00176 
00177     //@}
00178 
00179 
00180     /// @name Main init/execute/finalise
00181     //@{
00182 
00183     /// @deprecated Obsolete method, kept only for backwards compatibility
00184     void init() {}
00185 
00186 
00187     /// Initialize a run, with the run beams taken from the example event.
00188     void init(const GenEvent& event);
00189 
00190 
00191     /// Analyze the given \a event. This function will call the
00192     /// AnalysisBase::analyze() function of all included analysis objects.
00193     void analyze(const GenEvent& event);
00194 
00195 
00196     /// Finalize a run. This function first calls the AnalysisBase::finalize()
00197     /// functions of all included analysis objects and converts all histograms
00198     /// to AIDA DataPointSet objects in the AIDA tree. Using the histogram tree
00199     /// for further analysis or writing to file is left to the API user.
00200     void finalize();
00201 
00202     //@}
00203 
00204 
00205     /// @name AIDA factories etc.
00206     /// @deprecated All this will be removed when histogramming is overhauled
00207     //@{
00208 
00209     /// The AIDA analysis factory.
00210     /// @deprecated When AIDA goes, this goes...
00211     AIDA::IAnalysisFactory& analysisFactory();
00212 
00213 
00214     /// Commit the AIDA tree to file.
00215     /// @deprecated When AIDA goes, this goes...
00216     void commitData();
00217 
00218 
00219     /// Write the AIDA tree to the named file.
00220     /// @deprecated When AIDA goes, this goes...
00221     void writeData(const std::string& filename);
00222 
00223 
00224     /// The AIDA tree object.
00225     /// @deprecated When AIDA goes, this goes...
00226     AIDA::ITree& tree();
00227 
00228 
00229     /// The AIDA histogram factory.
00230     /// @deprecated When AIDA goes, this goes...
00231     AIDA::IHistogramFactory& histogramFactory();
00232 
00233 
00234     /// The AIDA histogram factory.
00235     /// @deprecated When AIDA goes, this goes...
00236     AIDA::IDataPointSetFactory& datapointsetFactory();
00237 
00238     //@}
00239 
00240 
00241   private:
00242 
00243     /// The collection of Analysis objects to be used.
00244     set<AnaHandle, AnaHandleLess> _analyses;
00245 
00246 
00247     /// @name Run properties
00248     //@{
00249 
00250     /// Run name
00251     std::string _runname;
00252 
00253     /// Number of events seen.
00254     size_t _numEvents;
00255 
00256     /// Sum of event weights seen.
00257     double _sumOfWeights;
00258 
00259     /// Cross-section known to AH
00260     double _xs;
00261 
00262     /// Beams used by this run.
00263     ParticlePair _beams;
00264 
00265     /// Flag to check if init has been called
00266     bool _initialised;
00267 
00268     /// Flag whether input event beams should be ignored in compatibility check
00269     bool _ignoreBeams;
00270 
00271     //@}
00272 
00273 
00274     /// @name AIDA factory handles
00275     /// Note that only the analysis factory can be a shared_ptr, since it deletes all the others.
00276     //@{
00277 
00278     /// The AIDA analysis factory.
00279     shared_ptr<AIDA::IAnalysisFactory> _theAnalysisFactory;
00280 
00281     /// The AIDA tree factory.
00282     AIDA::ITreeFactory* _theTreeFactory;
00283 
00284     /// The AIDA tree object.
00285     AIDA::ITree* _theTree;
00286 
00287     /// The AIDA histogram factory.
00288     AIDA::IHistogramFactory* _theHistogramFactory;
00289 
00290     /// The AIDA data point set factory.
00291     AIDA::IDataPointSetFactory* _theDataPointSetFactory;
00292 
00293     //@}
00294 
00295 
00296   private:
00297 
00298     /// The assignment operator is private and must never be called.
00299     /// In fact, it should not even be implemented.
00300     AnalysisHandler& operator=(const AnalysisHandler&);
00301 
00302     /// The copy constructor is private and must never be called.  In
00303     /// fact, it should not even be implemented.
00304     AnalysisHandler(const AnalysisHandler&);
00305 
00306   };
00307 
00308 
00309 }
00310 
00311 #endif