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/Logging.fhh"
00007 #include "Rivet/AnalysisHandler.fhh"
00008 #include "Rivet/Analysis.fhh"
00009 #include "Rivet/Event.fhh"
00010 #include "Rivet/AnalysisLoader.hh"
00011 
00012 namespace Rivet {
00013 
00014 
00015   /// A class which handles a number of analysis objects to be applied to
00016   /// generated events. An {@link Analysis}' AnalysisHandler is also responsible
00017   /// for handling the final writing-out of histograms.
00018   class AnalysisHandler {
00019 
00020   public:
00021 
00022     /// @name Standard constructors and destructors. */
00023     //@{
00024     /// The standard constructor.
00025     /// @param basefilename the name of the file (no extension) where histograms
00026     ///   are to be stored.
00027     /// @param runname optional name of this run, prepended to AIDA data paths.
00028     /// @param storetype a string indicating to the AIDA analysis factory
00029     ///   how to store the histograms. Which strings are allowed depends on
00030     ///   actual AIDA implementation used. To output in standard AIDA XML the
00031     ///   string is typically "xml".
00032     /// @param afac an AIDA analysis factory object. The caller must make
00033     ///   sure that the lifetime of the factory object exceeds the AnalysisHandler
00034     ///   object.
00035     AnalysisHandler(AIDA::IAnalysisFactory& afac, string basefilename="Rivet",
00036                     string runname="", HistoFormat storetype=AIDAML);
00037 
00038     /// Make a Rivet handler with a set base filename and store type.
00039     AnalysisHandler(string basefilename="Rivet",
00040                     string runname="", HistoFormat storetype=AIDAML);
00041 
00042     /// The destructor is not virtual as this class should not be inherited from.
00043     ~AnalysisHandler();
00044 
00045     //@}
00046 
00047 
00048   private:
00049 
00050     /// Do the initialisation of the AIDA analysis factories.
00051     void _setupFactories(string basefilename, HistoFormat storetype);
00052 
00053     /// Convert any IHistogram1D objects in the AIDA tree to IDataPointSet objects.
00054     void _normalizeTree(AIDA::ITree& tree);
00055 
00056     /// Get a logger object.
00057     Log& getLog();
00058 
00059 
00060   public:
00061 
00062     /// @name Run properties 
00063     //@{
00064 
00065     /// Get the name of this run.
00066     string runName() const;
00067 
00068 
00069     /// Get the number of events seen. Should only really be used by external
00070     /// steering code or analyses in the finalize phase.
00071     size_t numEvents() const;
00072 
00073     /// Get the sum of the event weights seen - the weighted equivalent of the
00074     /// number of events. Should only really be used by external steering code
00075     /// or analyses in the finalize phase.
00076     double sumOfWeights() const;
00077 
00078     /// Set sum of weights. This is useful if Rivet is steered externally and
00079     /// the analyses are run for a sub-contribution of the events
00080     /// (but of course have to be normalised to the total sum of weights)
00081     void setSumOfWeights(const double& sum);   
00082 
00083 
00084     /// Is cross-section information required by at least one child analysis?
00085     bool needCrossSection() const;
00086 
00087     /// Set the cross-section for the process being generated.
00088     AnalysisHandler& setCrossSection(double xs);
00089 
00090     /// Get the cross-section known to the handler.
00091     double crossSection() const {
00092       return _xs;
00093     }
00094 
00095     /// Whether the handler knows about a cross-section.
00096     bool hasCrossSection() const;
00097 
00098 
00099     /// Set beams for this run
00100     AnalysisHandler& setRunBeams(const ParticlePair& beams) { 
00101       _beams = beams;
00102       getLog() << Log::DEBUG << "Setting run beams = " << beams
00103                << " @ " << sqrtS()/GeV << " GeV" << endl;
00104       return *this;
00105     }
00106 
00107     /// Get beam IDs for this run, determined from first event
00108     const ParticlePair& beams() const { 
00109       return _beams;
00110     }
00111 
00112     /// Get beam IDs for this run, determined from first event
00113     BeamPair beamIds() const;
00114 
00115     /// Get energy for this run, determined from first event
00116     double sqrtS() const;
00117     
00118     //@}
00119 
00120 
00121     /// @name Handle analyses
00122     //@{
00123 
00124     /// Get a list of the currently registered analyses' names.
00125     std::vector<std::string> analysisNames() const;
00126 
00127     /// Get a list of the currently registered analyses' names.
00128     const std::set<Analysis*>& analyses() const {
00129       return _analyses;
00130     }
00131 
00132     /// Add an analysis to the run list using its name. The actual Analysis
00133     /// to be used will be obtained via AnalysisHandler::getAnalysis(string).
00134     /// If no matching analysis is found, no analysis is added (i.e. the
00135     /// null pointer is checked and discarded.
00136     AnalysisHandler& addAnalysis(const std::string& analysisname);
00137 
00138     /// Remove an analysis from the run list using its name.
00139     AnalysisHandler& removeAnalysis(const std::string& analysisname);
00140 
00141 
00142     /// Add analyses to the run list using their names. The actual {@link
00143     /// Analysis}' to be used will be obtained via
00144     /// AnalysisHandler::addAnalysis(string), which in turn uses
00145     /// AnalysisHandler::getAnalysis(string). If no matching analysis is found
00146     /// for a given name, no analysis is added, but also no error is thrown.
00147     AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
00148 
00149     /// Remove analyses from the run list using their names.
00150     AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
00151 
00152 
00153     /// Add an analysis to the run list by object
00154     AnalysisHandler& addAnalysis(Analysis* analysis);
00155 
00156     /// Remove beam-incompatible analyses from the run list.
00157     /// @todo Do this automatically in the init phase (including energies) and deprecate explicit use
00158     AnalysisHandler& removeIncompatibleAnalyses(const BeamPair& beams);
00159 
00160     //@}
00161 
00162 
00163     /// @name Main init/execute/finalise
00164     //@{
00165 
00166     /// @deprecated Obsolete method, kept only for backwards compatibility
00167     void init() {}
00168 
00169 
00170     /// Initialize a run, with the run beams taken from the example event.
00171     void init(const GenEvent& event);
00172 
00173 
00174     /// Analyze the given \a event. This function will call the
00175     /// AnalysisBase::analyze() function of all included analysis objects.
00176     void analyze(const GenEvent& event);
00177 
00178 
00179     /// Finalize a run. This function first calls the AnalysisBase::finalize()
00180     /// functions of all included analysis objects and converts all histograms
00181     /// to AIDA DataPointSet objects in the AIDA tree. Using the histogram tree
00182     /// for further analysis or writing to file is left to the API user.
00183     void finalize();
00184 
00185     //@}
00186 
00187 
00188     /// @name AIDA factories etc.
00189     /// @deprecated All this will be removed when histogramming is overhauled
00190     //@{
00191 
00192     /// The AIDA analysis factory.
00193     AIDA::IAnalysisFactory& analysisFactory();
00194 
00195 
00196     /// Commit the AIDA tree to file.
00197     void commitData();
00198  
00199 
00200     /// The AIDA tree object.
00201     AIDA::ITree& tree();
00202 
00203  
00204     /// The AIDA histogram factory.
00205     AIDA::IHistogramFactory& histogramFactory();
00206 
00207 
00208     /// The AIDA histogram factory.
00209     AIDA::IDataPointSetFactory& datapointsetFactory();
00210 
00211     //@}
00212 
00213 
00214   private:
00215 
00216     /// The collection of Analysis objects to be used.
00217     set<Analysis*> _analyses;
00218 
00219 
00220     /// @name Run properties
00221     //@{
00222 
00223     /// Run name
00224     std::string _runname;
00225  
00226     /// Number of events seen.
00227     size_t _numEvents;
00228 
00229     /// Sum of event weights seen.
00230     double _sumOfWeights;
00231 
00232     /// Cross-section known to AH
00233     double _xs;
00234 
00235     /// Beams used by this run.
00236     ParticlePair _beams;
00237 
00238     /// Flag to check if init has been called
00239     bool _initialised;
00240 
00241     //@}
00242 
00243 
00244     /// @name AIDA factory handles
00245     //@{
00246 
00247     /// The AIDA analysis factory.
00248     AIDA::IAnalysisFactory* _theAnalysisFactory;
00249 
00250     /// The AIDA tree object.
00251     AIDA::ITree* _theTree;
00252 
00253     /// The AIDA histogram factory.
00254     AIDA::IHistogramFactory* _theHistogramFactory;
00255 
00256     /// The AIDA data point set factory.
00257     AIDA::IDataPointSetFactory* _theDataPointSetFactory;
00258 
00259     //@}
00260 
00261 
00262   private:
00263 
00264     /// The assignment operator is private and must never be called.
00265     /// In fact, it should not even be implemented.
00266     AnalysisHandler& operator=(const AnalysisHandler&);
00267 
00268     /// The copy constructor is private and must never be called.  In
00269     /// fact, it should not even be implemented.
00270     AnalysisHandler(const AnalysisHandler&);
00271 
00272   };
00273 
00274 
00275 }
00276 
00277 #endif