rivet is hosted by Hepforge, IPPP Durham

#include <AnalysisHandler.hh>

Collaboration diagram for AnalysisHandler:

List of all members.

Public Member Functions

void writeData (const std::string &filename)
 Write all analyses' plots to the named file.
Constructors and destructors. */
 AnalysisHandler (const string &runname="")
 Preferred constructor, with optional run name.
 ~AnalysisHandler ()
 Destructor The destructor is not virtual, as this class should not be inherited from.
Handle analyses
std::vector< std::string > analysisNames () const
 Get a list of the currently registered analyses' names.
const std::set< AnaHandle,
AnaHandleLess > & 
analyses () const
 Get the collection of currently registered analyses.
AnalysisHandleraddAnalysis (const std::string &analysisname)
AnalysisHandlerremoveAnalysis (const std::string &analysisname)
 Remove an analysis from the run list using its name.
AnalysisHandleraddAnalyses (const std::vector< std::string > &analysisnames)
AnalysisHandlerremoveAnalyses (const std::vector< std::string > &analysisnames)
 Remove analyses from the run list using their names.
AnalysisHandleraddAnalysis (Analysis *analysis)
 Add an analysis to the run list by object.
Main init/execute/finalise
void init (const GenEvent &event)
 Initialize a run, with the run beams taken from the example event.
void analyze (const GenEvent &event)
void finalize ()

Private Member Functions

LoggetLog () const
 Get a logger object.
AnalysisHandleroperator= (const AnalysisHandler &)
 AnalysisHandler (const AnalysisHandler &)

Private Attributes

set< AnaHandle, AnaHandleLess_analyses
 The collection of Analysis objects to be used.

Run properties

std::string _runname
 Run name.
unsigned int _numEvents
 Number of events seen.
double _sumOfWeights
 Sum of event weights seen.
double _xs
 Cross-section known to AH.
ParticlePair _beams
 Beams used by this run.
bool _initialised
 Flag to check if init has been called.
bool _ignoreBeams
 Flag whether input event beams should be ignored in compatibility check.
string runName () const
 Get the name of this run.
size_t numEvents () const
double sumOfWeights () const
void setSumOfWeights (const double &sum)
bool needCrossSection () const
 Is cross-section information required by at least one child analysis?
AnalysisHandlersetCrossSection (double xs)
 Set the cross-section for the process being generated.
double crossSection () const
 Get the cross-section known to the handler.
bool hasCrossSection () const
 Whether the handler knows about a cross-section.
AnalysisHandlersetRunBeams (const ParticlePair &beams)
 Set beams for this run.
const ParticlePairbeams () const
 Get beam IDs for this run, usually determined from the first event.
PdgIdPair beamIds () const
 Get beam IDs for this run, usually determined from the first event.
double sqrtS () const
 Get energy for this run, usually determined from the first event.
void setIgnoreBeams (bool ignore=true)
 Setter for _ignoreBeams.

Detailed Description

A class which handles a number of analysis objects to be applied to generated events. An Analysis' AnalysisHandler is also responsible for handling the final writing-out of histograms.

Definition at line 29 of file AnalysisHandler.hh.


Constructor & Destructor Documentation

AnalysisHandler ( const string &  runname = "")

Preferred constructor, with optional run name.

Definition at line 21 of file AnalysisHandler.cc.

    : _runname(runname), _numEvents(0),
      _sumOfWeights(0.0), _xs(-1.0),
      _initialised(false), _ignoreBeams(false)
  {}

Destructor The destructor is not virtual, as this class should not be inherited from.

Definition at line 27 of file AnalysisHandler.cc.

  {}
AnalysisHandler ( const AnalysisHandler ) [private]

The copy constructor is private and must never be called. In fact, it should not even be implemented.


Member Function Documentation

AnalysisHandler & addAnalyses ( const std::vector< std::string > &  analysisnames)

Add analyses to the run list using their names. The actual Analysis' to be used will be obtained via AnalysisHandler::addAnalysis(string), which in turn uses AnalysisHandler::getAnalysis(string). If no matching analysis is found for a given name, no analysis is added, but also no error is thrown.

Definition at line 242 of file AnalysisHandler.cc.

References AnalysisHandler::addAnalysis().

                                                                                         {
    foreach (const string& aname, analysisnames) {
      //MSG_DEBUG("Adding analysis '" << aname << "'");
      addAnalysis(aname);
    }
    return *this;
  }
AnalysisHandler & addAnalysis ( const std::string &  analysisname)

Add an analysis to the run list using its name. The actual Analysis to be used will be obtained via AnalysisHandler::getAnalysis(string). If no matching analysis is found, no analysis is added (i.e. the null pointer is checked and discarded.

Todo:
Might we want to be able to run an analysis twice, with different params? Requires avoiding histo tree clashes, i.e. storing the histos on the analysis objects.

Definition at line 166 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses, AnalysisLoader::getAnalysis(), MSG_DEBUG, and MSG_WARNING.

Referenced by AnalysisHandler::addAnalyses().

                                                                          {
    // Check for a duplicate analysis
    /// @todo Might we want to be able to run an analysis twice, with different params?
    ///       Requires avoiding histo tree clashes, i.e. storing the histos on the analysis objects.
    foreach (const AnaHandle& a, _analyses) {
      if (a->name() == analysisname) {
        MSG_WARNING("Analysis '" << analysisname << "' already registered: skipping duplicate");
        return *this;
      }
    }
    AnaHandle analysis( AnalysisLoader::getAnalysis(analysisname) );
    if (analysis.get() != 0) { // < Check for null analysis.
      MSG_DEBUG("Adding analysis '" << analysisname << "'");
      analysis->_analysishandler = this;
      _analyses.insert(analysis);
    } else {
      MSG_WARNING("Analysis '" << analysisname << "' not found.");
    }
    // MSG_WARNING(_analyses.size());
    // foreach (const AnaHandle& a, _analyses) MSG_WARNING(a->name());
    return *this;
  }

Add an analysis to the run list by object.

Definition at line 283 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses, and Analysis::_analysishandler.

                                                                  {
    analysis->_analysishandler = this;
    _analyses.insert(AnaHandle(analysis));
    return *this;
  }
const std::set<AnaHandle, AnaHandleLess>& analyses ( ) const [inline]

Get the collection of currently registered analyses.

Definition at line 121 of file AnalysisHandler.hh.

References AnalysisHandler::_analyses.

Referenced by AnalysisHandler::init().

                                                             {
      return _analyses;
    }
std::vector< std::string > analysisNames ( ) const

Get a list of the currently registered analyses' names.

Definition at line 233 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses.

Referenced by Run::init(), and AnalysisHandler::init().

                                                            {
    std::vector<std::string> rtn;
    foreach (AnaHandle a, _analyses) {
      rtn.push_back(a->name());
    }
    return rtn;
  }
void analyze ( const GenEvent &  event)

Analyze the given event. This function will call the AnalysisBase::analyze() function of all included analysis objects.

Definition at line 95 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses, AnalysisHandler::_beams, AnalysisHandler::_initialised, AnalysisHandler::_numEvents, AnalysisHandler::_sumOfWeights, AnalysisHandler::beamIds(), AnalysisHandler::beams(), Rivet::compatible(), Rivet::fuzzyEquals(), Rivet::GeV, AnalysisHandler::init(), MSG_DEBUG, AnalysisHandler::setCrossSection(), AnalysisHandler::sqrtS(), and Rivet::PID::toBeamsString().

Referenced by Run::processEvent().

                                                  {
    // Call init with event as template if not already initialised
    if (!_initialised) {
      init(ge);
    }
    // Proceed with event analysis
    assert(_initialised);
    // Ensure that beam details match those from first event
    const PdgIdPair beams = Rivet::beamIds(ge);
    const double sqrts = Rivet::sqrtS(ge);
    if (!compatible(beams, _beams) || !fuzzyEquals(sqrts, sqrtS())) {
      cerr << "Event beams mismatch: "
           << PID::toBeamsString(beams) << " @ " << sqrts/GeV << " GeV" << " vs. first beams "
           << this->beams() << " @ " << this->sqrtS()/GeV << " GeV" << endl;
      exit(1);
    }


    Event event(ge);
    _numEvents++;
    // Weights
    const double weight = event.weight();
    _sumOfWeights += weight;
    MSG_DEBUG("Event #" << _numEvents << " weight = " << weight);
    #ifdef HEPMC_HAS_CROSS_SECTION
    if (ge.cross_section()) {
      const double xs = ge.cross_section()->cross_section();
      setCrossSection(xs);
    }
    #endif
    foreach (AnaHandle a, _analyses) {
      //MSG_DEBUG("About to run analysis " << a->name());
      try {
        a->analyze(event);
      } catch (const Error& err) {
        cerr     << "Error in " << a->name() << "::analyze method: "
                 << err.what() << endl;
        exit(1);
      }
      //MSG_DEBUG("Finished running analysis " << a->name());
    }
  }
PdgIdPair beamIds ( ) const

Get beam IDs for this run, usually determined from the first event.

Definition at line 290 of file AnalysisHandler.cc.

References AnalysisHandler::beams().

Referenced by AnalysisHandler::analyze(), and Analysis::beamIds().

                                           {
    return Rivet::beamIds(beams());
  }
const ParticlePair& beams ( ) const [inline]

Get beam IDs for this run, usually determined from the first event.

Definition at line 98 of file AnalysisHandler.hh.

References AnalysisHandler::_beams.

Referenced by AnalysisHandler::analyze(), AnalysisHandler::beamIds(), Analysis::beams(), AnalysisHandler::init(), AnalysisHandler::setRunBeams(), and AnalysisHandler::sqrtS().

                                      {
      return _beams;
    }
double crossSection ( ) const [inline]

Get the cross-section known to the handler.

Definition at line 82 of file AnalysisHandler.hh.

References AnalysisHandler::_xs.

Referenced by Run::crossSection(), and AnalysisHandler::hasCrossSection().

                                {
      return _xs;
    }
void finalize ( )

Finalize a run. This function calls the AnalysisBase::finalize() functions of all included analysis objects.

Definition at line 139 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses, AnalysisHandler::_initialised, AnalysisHandler::_numEvents, and MSG_INFO.

                                 {
    if (!_initialised) return;
    MSG_INFO("Finalising analyses");
    foreach (AnaHandle a, _analyses) {
      try {
        a->finalize();
      } catch (const Error& err) {
        cerr << "Error in " << a->name() << "::finalize method: "
             << err.what() << endl;
        exit(1);
      }
    }

    // Print out number of events processed
    MSG_INFO("Processed " << _numEvents << " event" << (_numEvents == 1 ? "" : "s"));

    // // Delete analyses
    // MSG_DEBUG("Deleting analyses");
    // _analyses.clear();

    // Print out MCnet boilerplate
    cout << endl;
    cout << "The MCnet usage guidelines apply to Rivet: see http://www.montecarlonet.org/GUIDELINES" << endl;
    cout << "Please acknowledge plots made with Rivet analyses, and cite arXiv:1003.0694 (http://arxiv.org/abs/1003.0694)" << endl;
  }
Log & getLog ( ) const [private]

Get a logger object.

Definition at line 31 of file AnalysisHandler.cc.

                                     {
    return Log::getLog("Rivet.Analysis.Handler");
  }
bool hasCrossSection ( ) const

Whether the handler knows about a cross-section.

Definition at line 278 of file AnalysisHandler.cc.

References AnalysisHandler::crossSection().

Referenced by Run::processEvent().

                                              {
    return (!std::isnan(crossSection()));
  }
void init ( const GenEvent &  event)

Initialize a run, with the run beams taken from the example event.

Definition at line 36 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses, AnalysisHandler::_ignoreBeams, AnalysisHandler::_initialised, AnalysisHandler::_numEvents, AnalysisHandler::_sumOfWeights, AnalysisHandler::analyses(), AnalysisHandler::analysisNames(), Rivet::beams(), AnalysisHandler::beams(), MSG_DEBUG, MSG_WARNING, AnalysisHandler::removeAnalysis(), AnalysisHandler::setRunBeams(), and Rivet::toUpper().

Referenced by AnalysisHandler::analyze(), and Run::init().

                                               {
    if (_initialised)
      throw UserError("AnalysisHandler::init has already been called: cannot re-initialize!");

    setRunBeams(Rivet::beams(ge));
    MSG_DEBUG("Initialising the analysis handler");
    _numEvents = 0;
    _sumOfWeights = 0.0;

    // Check that analyses are beam-compatible, and remove those that aren't
    const size_t num_anas_requested = analysisNames().size();
    vector<string> anamestodelete;
    foreach (const AnaHandle a, _analyses) {
      if (!_ignoreBeams && !a->isCompatible(beams())) {
        //MSG_DEBUG(a->name() << " requires beams " << a->requiredBeams() << " @ " << a->requiredEnergies() << " GeV");
        anamestodelete.push_back(a->name());
      }
    }
    foreach (const string& aname, anamestodelete) {
      MSG_WARNING("Analysis '" << aname << "' is incompatible with the provided beams: removing");
      removeAnalysis(aname);
    }
    if (num_anas_requested > 0 && analysisNames().empty()) {
      cerr << "All analyses were incompatible with the first event's beams\n"
           << "Exiting, since this probably wasn't intentional!" << endl;
      exit(1);
    }

    // Warn if any analysis' status is not unblemished
    foreach (const AnaHandle a, analyses()) {
      if (toUpper(a->status()) == "PRELIMINARY") {
        MSG_WARNING("Analysis '" << a->name() << "' is preliminary: be careful, it may change and/or be renamed!");
      } else if (toUpper(a->status()) == "OBSOLETE") {
        MSG_WARNING("Analysis '" << a->name() << "' is obsolete: please update!");
      } else if (toUpper(a->status()).find("UNVALIDATED") != string::npos) {
        MSG_WARNING("Analysis '" << a->name() << "' is unvalidated: be careful, it may be broken!");
      }
    }

    // Initialize the remaining analyses
    foreach (AnaHandle a, _analyses) {
      MSG_DEBUG("Initialising analysis: " << a->name());
      try {
        // Allow projection registration in the init phase onwards
        a->_allowProjReg = true;
        a->init();
        //MSG_DEBUG("Checking consistency of analysis: " << a->name());
        //a->checkConsistency();
      } catch (const Error& err) {
        cerr << "Error in " << a->name() << "::init method: " << err.what() << endl;
        exit(1);
      }
      MSG_DEBUG("Done initialising analysis: " << a->name());
    }
    _initialised = true;
    MSG_DEBUG("Analysis handler initialised");
  }
bool needCrossSection ( ) const

Is cross-section information required by at least one child analysis?

Definition at line 259 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses.

Referenced by Run::processEvent().

                                               {
    bool rtn = false;
    foreach (const AnaHandle a, _analyses) {
      if (!rtn) rtn = a->needsCrossSection();
      if (rtn) break;
    }
    return rtn;
  }
size_t numEvents ( ) const

Get the number of events seen. Should only really be used by external steering code or analyses in the finalize phase.

Definition at line 224 of file AnalysisHandler.cc.

References AnalysisHandler::_numEvents.

Referenced by Analysis::numEvents().

{ return _numEvents; }
AnalysisHandler& operator= ( const AnalysisHandler ) [private]

The assignment operator is private and must never be called. In fact, it should not even be implemented.

AnalysisHandler & removeAnalyses ( const std::vector< std::string > &  analysisnames)

Remove analyses from the run list using their names.

Definition at line 251 of file AnalysisHandler.cc.

References AnalysisHandler::removeAnalysis().

                                                                                            {
    foreach (const string& aname, analysisnames) {
      removeAnalysis(aname);
    }
    return *this;
  }
AnalysisHandler & removeAnalysis ( const std::string &  analysisname)

Remove an analysis from the run list using its name.

Definition at line 190 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses, and MSG_DEBUG.

Referenced by AnalysisHandler::init(), and AnalysisHandler::removeAnalyses().

                                                                             {
    shared_ptr<Analysis> toremove;
    foreach (const AnaHandle a, _analyses) {
      if (a->name() == analysisname) {
        toremove = a;
        break;
      }
    }
    if (toremove.get() != 0) {
      MSG_DEBUG("Removing analysis '" << analysisname << "'");
      _analyses.erase(toremove);
    }
    return *this;
  }
string runName ( ) const

Get the name of this run.

Definition at line 223 of file AnalysisHandler.cc.

References AnalysisHandler::_runname.

Referenced by Analysis::histoDir().

{ return _runname; }
AnalysisHandler & setCrossSection ( double  xs)

Set the cross-section for the process being generated.

Definition at line 269 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses, and AnalysisHandler::_xs.

Referenced by AnalysisHandler::analyze(), Run::init(), and Run::processEvent().

                                                             {
    _xs = xs;
    foreach (AnaHandle a, _analyses) {
      a->setCrossSection(xs);
    }
    return *this;
  }
void setIgnoreBeams ( bool  ignore = true)

Setter for _ignoreBeams.

Definition at line 299 of file AnalysisHandler.cc.

References AnalysisHandler::_ignoreBeams.

                                                  {
    _ignoreBeams=ignore;
  }
AnalysisHandler& setRunBeams ( const ParticlePair beams) [inline]

Set beams for this run.

Definition at line 91 of file AnalysisHandler.hh.

References AnalysisHandler::_beams, AnalysisHandler::beams(), Rivet::GeV, MSG_DEBUG, and AnalysisHandler::sqrtS().

Referenced by AnalysisHandler::init().

                                                            {
      _beams = beams;
      MSG_DEBUG("Setting run beams = " << beams << " @ " << sqrtS()/GeV << " GeV");
      return *this;
    }
void setSumOfWeights ( const double &  sum)

Set sum of weights. This is useful if Rivet is steered externally and the analyses are run for a sub-contribution of the events (but of course have to be normalised to the total sum of weights)

Definition at line 228 of file AnalysisHandler.cc.

References AnalysisHandler::_sumOfWeights.

                                                         {
    _sumOfWeights=sum;
  }
double sqrtS ( ) const

Get energy for this run, usually determined from the first event.

Definition at line 295 of file AnalysisHandler.cc.

References AnalysisHandler::beams().

Referenced by AnalysisHandler::analyze(), AnalysisHandler::setRunBeams(), and Analysis::sqrtS().

                                      {
    return Rivet::sqrtS(beams());
  }
double sumOfWeights ( ) const

Get the sum of the event weights seen - the weighted equivalent of the number of events. Should only really be used by external steering code or analyses in the finalize phase.

Definition at line 225 of file AnalysisHandler.cc.

References AnalysisHandler::_sumOfWeights.

Referenced by Analysis::sumOfWeights().

{ return _sumOfWeights; }
void writeData ( const std::string &  filename)

Write all analyses' plots to the named file.

Definition at line 206 of file AnalysisHandler.cc.

References AnalysisHandler::_analyses.

                                                        {
    vector<AnalysisObjectPtr> all_aos;
    foreach (const AnaHandle a, _analyses) {
      vector<AnalysisObjectPtr> aos = a->analysisObjects();
      // MSG_WARNING(a->name() << " " << aos.size());
      sort(aos.begin(), aos.end(), AOSortByPath);
      foreach (const AnalysisObjectPtr ao, aos) {
        if (ao->path().find("/TMP/") != string::npos) continue;
        all_aos.push_back(ao);
      }
    }
    // MSG_WARNING("Number of output analysis objects = " << all_aos.size());
    // foreach (const AnalysisObjectPtr ao, all_aos) MSG_WARNING(ao->path());
    WriterYODA::write(filename, all_aos.begin(), all_aos.end());
  }

Member Data Documentation

ParticlePair _beams [private]

Beams used by this run.

Definition at line 197 of file AnalysisHandler.hh.

Referenced by AnalysisHandler::analyze(), AnalysisHandler::beams(), and AnalysisHandler::setRunBeams().

bool _ignoreBeams [private]

Flag whether input event beams should be ignored in compatibility check.

Definition at line 203 of file AnalysisHandler.hh.

Referenced by AnalysisHandler::init(), and AnalysisHandler::setIgnoreBeams().

bool _initialised [private]

Flag to check if init has been called.

Definition at line 200 of file AnalysisHandler.hh.

Referenced by AnalysisHandler::analyze(), AnalysisHandler::finalize(), and AnalysisHandler::init().

unsigned int _numEvents [private]
std::string _runname [private]

Run name.

Definition at line 185 of file AnalysisHandler.hh.

Referenced by AnalysisHandler::runName().

double _sumOfWeights [private]
double _xs [private]

Cross-section known to AH.

Definition at line 194 of file AnalysisHandler.hh.

Referenced by AnalysisHandler::crossSection(), and AnalysisHandler::setCrossSection().


The documentation for this class was generated from the following files: