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