Rivet  3.1.0
AnalysisHandler.hh
1 // -*- C++ -*-
2 #ifndef RIVET_RivetHandler_HH
3 #define RIVET_RivetHandler_HH
4 
5 #include "Rivet/Config/RivetCommon.hh"
6 #include "Rivet/Particle.hh"
7 #include "Rivet/AnalysisLoader.hh"
8 #include "Rivet/Tools/RivetYODA.hh"
9 
10 namespace Rivet {
11 
12 
13  // Forward declaration and smart pointer for Analysis
14  class Analysis;
15  typedef std::shared_ptr<Analysis> AnaHandle;
16 
17 
24  public:
25 
27 
28 
30  AnalysisHandler(const string& runname="");
31 
35 
37 
38 
39  private:
40 
42  Log& getLog() const;
43 
44 
45  public:
46 
48 
49 
51  string runName() const;
52 
55  size_t numEvents() const;
56 
61  double sumW() const { return _eventCounter->sumW(); }
63  double sumW2() const { return _eventCounter->sumW2(); }
64 
66  const vector<string>& weightNames() const { return _weightNames; }
67 
69  size_t numWeights() const { return _weightNames.size(); }
70 
72  bool haveNamedWeights() const;
73 
75  void setWeightNames(const GenEvent& ge);
76 
78  size_t defaultWeightIndex() const { return _defaultWeightIdx; }
79 
81  void setWeightCap(const double maxWeight) { _weightCap = maxWeight; }
82 
84 
85 
87 
88 
90  Scatter1DPtr crossSection() const { return _xs; }
91 
93  void setCrossSection(pair<double, double> xsec, bool isUserSupplied=false);
95  void setCrossSection(double xsec, double xsecerr, bool isUserSupplied=false) {
96  setCrossSection({xsec, xsecerr}, isUserSupplied);
97  }
98 
100  double nominalCrossSection() const {
101  _xs.get()->setActiveWeightIdx(_defaultWeightIdx);
102  const YODA::Scatter1D::Points& ps = _xs->points();
103  if (ps.size() != 1) {
104  string errMsg = "cross section missing when requesting nominal cross section";
105  throw Error(errMsg);
106  }
107  double xs = ps[0].x();
108  _xs.get()->unsetActiveWeight();
109  return xs;
110  }
111 
113 
114 
116 
117 
119  AnalysisHandler& setRunBeams(const ParticlePair& beams) {
120  _beams = beams;
121  MSG_DEBUG("Setting run beams = " << beams << " @ " << sqrtS()/GeV << " GeV");
122  return *this;
123  }
124 
126  const ParticlePair& beams() const { return _beams; }
127 
130  PdgIdPair beamIds() const;
131 
134  double sqrtS() const;
135 
137  void setIgnoreBeams(bool ignore=true);
138 
140  void skipMultiWeights(bool ignore=false);
141 
143 
144 
146 
147 
149  std::vector<std::string> analysisNames() const;
150 
152  const std::map<std::string, AnaHandle>& analysesMap() const {
153  return _analyses;
154  }
155 
157  std::vector<AnaHandle> analyses() const {
158  std::vector<AnaHandle> rtn;
159  rtn.reserve(_analyses.size());
160  for (const auto& apair : _analyses) rtn.push_back(apair.second);
161  return rtn;
162  }
163 
165  AnaHandle analysis(const std::string& analysisname) {
166  if ( _analyses.find(analysisname) == _analyses.end() )
167  throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
168  try {
169  return _analyses[analysisname];
170  } catch (...) {
171  throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
172  }
173  }
174 
177 
183  AnalysisHandler& addAnalysis(const std::string& analysisname);
184 
186  AnalysisHandler& addAnalysis(const std::string& analysisname, std::map<string, string> pars);
187 
194  AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
195 
196 
198  AnalysisHandler& removeAnalysis(const std::string& analysisname);
199 
201  AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
202 
204 
206 
207 
209 
210 
212  void init(const GenEvent& event);
213 
218  void analyze(const GenEvent& event);
219 
224  void analyze(const GenEvent* event);
225 
228  void finalize();
229 
231 
232 
234 
235 
238  void pushToPersistent();
239 
241  void readData(const std::string& filename);
242 
244  vector<MultiweightAOPtr> getRivetAOs() const;
245 
248  const YODA::AnalysisObjectPtr getPreload(string path) const {
249  auto it = _preloads.find(path);
250  if ( it == _preloads.end() ) return nullptr;
251  return it->second;
252  }
253 
255  void writeData(const std::string& filename) const;
256 
260  void dump(string dumpfile, int period) {
261  _dumpPeriod = period;
262  _dumpFile = dumpfile;
263  }
264 
279  void mergeYodas(const vector<string> & aofiles,
280  const vector<string> & delopts = vector<string>(),
281  bool equiv = false);
282 
284  void stripOptions(YODA::AnalysisObjectPtr ao,
285  const vector<string> & delopts) const;
286 
288 
289 
292  enum class Stage { OTHER, INIT, FINALIZE };
293 
295  Stage stage() const { return _stage; }
296 
297 
298  private:
299 
301  Stage _stage = Stage::OTHER;
302 
304  std::map<std::string, AnaHandle> _analyses;
305 
308  map<string,YODA::AnalysisObjectPtr> _preloads;
309 
312  vector<YODA::AnalysisObjectPtr> _finalizedAOs;
313 
314 
316 
317 
319  std::vector<std::string> _weightNames;
320  std::vector<std::valarray<double> > _subEventWeights;
321  //size_t _numWeightTypes; // always == WeightVector.size()
322 
324  std::string _runname;
325 
327  mutable CounterPtr _eventCounter;
328 
330  Scatter1DPtr _xs;
331 
333  std::pair<double,double> _userxs;
334 
336  ParticlePair _beams;
337 
339  bool _initialised;
340 
342  bool _ignoreBeams;
343 
345  bool _skipWeights;
346 
348  double _weightCap;
349 
351  int _eventNumber;
352 
354  size_t _defaultWeightIdx;
355 
358  int _dumpPeriod;
359 
362  string _dumpFile;
363 
365  bool _dumping;
366 
368 
369 
370  private:
371 
374  AnalysisHandler& operator=(const AnalysisHandler&);
375 
379 
380  };
381 
382 
383 }
384 
385 #endif
Definition: MC_Cent_pPb.hh:10
void setCrossSection(pair< double, double > xsec, bool isUserSupplied=false)
Set the cross-section for the process being generated.
void mergeYodas(const vector< string > &aofiles, const vector< string > &delopts=vector< string >(), bool equiv=false)
const ParticlePair & beams() const
Get the beam particles for this run, usually determined from the first event.
Definition: AnalysisHandler.hh:126
double nominalCrossSection() const
Get the nominal cross-section.
Definition: AnalysisHandler.hh:100
AnalysisHandler & addAnalysis(Analysis *analysis)
Add an analysis to the run list by object.
bool haveNamedWeights() const
Are any of the weights non-numeric?
void analyze(const GenEvent &event)
Analyze the given event by reference.
void init(const GenEvent &event)
Initialize a run, with the run beams taken from the example event.
double sumW2() const
Access to the sum of squared-weights.
Definition: AnalysisHandler.hh:63
void setWeightCap(const double maxWeight)
Set the weight cap.
Definition: AnalysisHandler.hh:81
void skipMultiWeights(bool ignore=false)
Setter for _skipWeights.
std::vector< AnaHandle > analyses() const
Get the collection of currently registered analyses.
Definition: AnalysisHandler.hh:157
std::vector< std::string > analysisNames() const
Get a list of the currently registered analyses&#39; names.
void setCrossSection(double xsec, double xsecerr, bool isUserSupplied=false)
Set the cross-section for the process being generated (alternative signature)
Definition: AnalysisHandler.hh:95
Scatter1DPtr crossSection() const
Get the cross-section known to the handler.
Definition: AnalysisHandler.hh:90
PdgIdPair beamIds() const
AnalysisHandler(const string &runname="")
Preferred constructor, with optional run name.
Stage
Definition: AnalysisHandler.hh:292
void readData(const std::string &filename)
Read analysis plots into the histo collection (via addData) from the named file.
Logging system for controlled & formatted writing to stdout.
Definition: Logging.hh:10
void setWeightNames(const GenEvent &ge)
Set the weight names from a GenEvent.
AnalysisHandler & removeAnalyses(const std::vector< std::string > &analysisnames)
Remove analyses from the run list using their names.
~AnalysisHandler()
Destructor The destructor is not virtual, as this class should not be inherited from.
Generic runtime Rivet error.
Definition: Exceptions.hh:12
AnalysisHandler & addAnalyses(const std::vector< std::string > &analysisnames)
Add analyses to the run list using their names.
This is the base class of all analysis classes in Rivet.
Definition: Analysis.hh:64
size_t numWeights() const
Are any of the weights non-numeric?
Definition: AnalysisHandler.hh:69
AnalysisHandler & removeAnalysis(const std::string &analysisname)
Remove an analysis from the run list using its name.
const vector< string > & weightNames() const
Names of event weight categories.
Definition: AnalysisHandler.hh:66
Stage stage() const
Which stage are we in?
Definition: AnalysisHandler.hh:295
Error relating to looking up analysis objects in the register.
Definition: Exceptions.hh:61
The key class for coordination of Analysis objects and the event loop.
Definition: AnalysisHandler.hh:23
void dump(string dumpfile, int period)
Definition: AnalysisHandler.hh:260
const std::map< std::string, AnaHandle > & analysesMap() const
Get the collection of currently registered analyses.
Definition: AnalysisHandler.hh:152
double sumW() const
Access the sum of the event weights seen.
Definition: AnalysisHandler.hh:61
const YODA::AnalysisObjectPtr getPreload(string path) const
Definition: AnalysisHandler.hh:248
double sqrtS() const
size_t numEvents() const
string runName() const
Get the name of this run.
void stripOptions(YODA::AnalysisObjectPtr ao, const vector< string > &delopts) const
Helper function to strip specific options from data object paths.
void writeData(const std::string &filename) const
Write all analyses&#39; plots (via getData) to the named file.
vector< MultiweightAOPtr > getRivetAOs() const
Get all multi-weight Rivet analysis object wrappers.
size_t defaultWeightIndex() const
Get the index of the nominal weight-stream.
Definition: AnalysisHandler.hh:78
void setIgnoreBeams(bool ignore=true)
Setter for _ignoreBeams.
AnaHandle analysis(const std::string &analysisname)
Get a registered analysis by name.
Definition: AnalysisHandler.hh:165
AnalysisHandler & setRunBeams(const ParticlePair &beams)
Set the beam particles for this run.
Definition: AnalysisHandler.hh:119