Rivet  3.1.3
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  AnalysisHandler(const string& runname="");
28 
30  AnalysisHandler(const AnalysisHandler&) = delete;
31 
33  AnalysisHandler& operator=(const AnalysisHandler&) = delete;
34 
37 
38 
41 
43  string runName() const;
44 
47  size_t numEvents() const;
48 
53  double sumW() const { return _eventCounter->sumW(); }
55  double sumW2() const { return _eventCounter->sumW2(); }
56 
58  const vector<string>& weightNames() const { return _weightNames; }
59 
61  //const vector<size_t> weightIndices() const { return _weightIndices; }
62 
64  size_t numWeights() const { return _weightNames.size(); }
65 
67  bool haveNamedWeights() const;
68 
70  void setWeightNames(const GenEvent& ge);
71 
73  size_t defaultWeightIndex() const { return _rivetDefaultWeightIdx; }
74 
76  void setWeightCap(const double maxWeight) { _weightCap = maxWeight; }
77 
79  void setNLOSmearing(double frac) { _NLOSmearing = frac; }
80 
82  void skipMultiWeights(bool ignore=false);
83 
85  void selectMultiWeights(std::string patterns="");
86 
88  void deselectMultiWeights(std::string patterns="");
89 
91  void setNominalWeightName(std::string name="");
92 
94 
95 
98 
100  Scatter1DPtr crossSection() const { return _xs; }
101 
103  void setCrossSection(const pair<double, double>& xsec, bool isUserSupplied=false);
105  void setCrossSection(double xsec, double xsecerr, bool isUserSupplied=false) {
106  setCrossSection({xsec, xsecerr}, isUserSupplied);
107  }
108 
110  double nominalCrossSection() const {
111  _xs.get()->setActiveWeightIdx(_rivetDefaultWeightIdx);
112  const YODA::Scatter1D::Points& ps = _xs->points();
113  if (ps.size() != 1) {
114  string errMsg = "value missing when requesting nominal cross-section";
115  throw Error(errMsg);
116  }
117  double xs = ps[0].x();
118  _xs.get()->unsetActiveWeight();
119  return xs;
120  }
121 
123 
124 
127 
129  AnalysisHandler& setRunBeams(const ParticlePair& beams) {
130  _beams = beams;
131  MSG_DEBUG("Setting run beams = " << beams << " @ " << sqrtS()/GeV << " GeV");
132  return *this;
133  }
134 
136  const ParticlePair& beams() const { return _beams; }
137 
140  PdgIdPair beamIds() const;
141 
144  double sqrtS() const;
145 
148  void checkBeams(bool check=true) { setIgnoreBeams(check); }
151  void setIgnoreBeams(bool ignore=true);
152 
154 
155 
158 
160  std::vector<std::string> analysisNames() const;
161 
163  std::vector<std::string> stdAnalysisNames() const;
164 
166  const std::map<std::string, AnaHandle>& analysesMap() const {
167  return _analyses;
168  }
169 
171  std::vector<AnaHandle> analyses() const {
172  std::vector<AnaHandle> rtn;
173  rtn.reserve(_analyses.size());
174  for (const auto& apair : _analyses) rtn.push_back(apair.second);
175  return rtn;
176  }
177 
179  AnaHandle analysis(const std::string& analysisname) {
180  if ( _analyses.find(analysisname) == _analyses.end() )
181  throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
182  try {
183  return _analyses[analysisname];
184  } catch (...) {
185  throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
186  }
187  }
188 
191 
197  AnalysisHandler& addAnalysis(const std::string& analysisname);
198 
200  AnalysisHandler& addAnalysis(const std::string& analysisname, std::map<string, string> pars);
201 
208  AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
209 
210 
212  AnalysisHandler& removeAnalysis(const std::string& analysisname);
213 
215  AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
216 
218 
219 
222 
224  void init(const GenEvent& event);
225 
230  void analyze(const GenEvent& event);
231 
236  void analyze(const GenEvent* event);
237 
240  void finalize();
241 
243 
244 
247 
249  void readData(const std::string& filename);
250 
252  vector<YODA::AnalysisObjectPtr> getYodaAOs(bool includeraw=false) const;
253 
256  const YODA::AnalysisObjectPtr getPreload(string path) const {
257  auto it = _preloads.find(path);
258  if ( it == _preloads.end() ) return nullptr;
259  return it->second;
260  }
261 
263  void writeData(const string& filename) const;
264 
270  void setAODump(const string& dumpfile, int period) {
271  dump(dumpfile, period);
272  }
274  void setNoAODump() {
275  setAODump("DUMMY", -1);
276  }
279  void dump(const string& dumpfile, int period) {
280  _dumpPeriod = period;
281  _dumpFile = dumpfile;
282  }
283 
298  void mergeYodas(const vector<string>& aofiles,
299  const vector<string>& delopts=vector<string>(),
300  const vector<string>& addopts=vector<string>(),
301  bool equiv=false);
302 
304 
305 
308 
311  enum class Stage { OTHER, INIT, FINALIZE };
312 
314  Stage stage() const { return _stage; }
315 
317 
318 
319  private:
320 
323 
325  Log& getLog() const;
326 
328  vector<MultiweightAOPtr> getRivetAOs() const;
329 
331  void stripOptions(YODA::AnalysisObjectPtr ao, const vector<string>& delopts) const;
332 
335  void pushToPersistent();
336 
338 
339 
340  private:
341 
343  Stage _stage = Stage::OTHER;
344 
346  std::map<std::string, AnaHandle> _analyses;
347 
351  map<string,YODA::AnalysisObjectPtr> _preloads;
352 
354  vector<YODA::AnalysisObjectPtr> _finalizedAOs;
355 
356 
359 
361  std::vector<std::string> _weightNames;
362  std::vector<std::valarray<double> > _subEventWeights;
363  //size_t _numWeightTypes; // always == WeightVector.size()
364 
366  std::vector<size_t> _weightIndices;
367 
369  std::string _runname;
370 
372  mutable CounterPtr _eventCounter;
373 
375  Scatter1DPtr _xs;
376 
378  std::pair<double,double> _userxs;
379 
381  ParticlePair _beams;
382 
384  bool _initialised;
385 
387  bool _ignoreBeams;
388 
390  bool _skipWeights;
391 
393  std::string _matchWeightNames;
394 
396  std::string _unmatchWeightNames;
397 
399  std::string _nominalWeightName;
400 
402  double _weightCap;
403 
407  double _NLOSmearing;
408 
410  int _eventNumber;
411 
413  size_t _defaultWeightIdx;
414 
416  size_t _rivetDefaultWeightIdx;
417 
419  int _dumpPeriod;
420 
422  string _dumpFile;
423 
425  bool _dumping;
426 
428 
429  };
430 
431 
432 }
433 
434 #endif
Definition: MC_Cent_pPb.hh:10
void deselectMultiWeights(std::string patterns="")
Setter for _unmatchWeightNames.
const ParticlePair & beams() const
Get the beam particles for this run, usually determined from the first event.
Definition: AnalysisHandler.hh:136
double nominalCrossSection() const
Get the nominal cross-section.
Definition: AnalysisHandler.hh:110
AnalysisHandler & addAnalysis(Analysis *analysis)
Add an analysis to the run list by object.
bool haveNamedWeights() const
Are any of the weights non-numeric?
vector< YODA::AnalysisObjectPtr > getYodaAOs(bool includeraw=false) const
Get all YODA analysis objects (across all weights, optionally including RAW)
void writeData(const string &filename) const
Write all analyses&#39; plots (via getData) to the named file.
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:55
void setWeightCap(const double maxWeight)
Set the weight cap.
Definition: AnalysisHandler.hh:76
void skipMultiWeights(bool ignore=false)
Setter for _skipWeights.
std::vector< AnaHandle > analyses() const
Get the collection of currently registered analyses.
Definition: AnalysisHandler.hh:171
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:105
Scatter1DPtr crossSection() const
Get the cross-section known to the handler.
Definition: AnalysisHandler.hh:100
PdgIdPair beamIds() const
#define MSG_DEBUG(x)
Debug messaging, not enabled by default, using MSG_LVL.
Definition: Logging.hh:195
void setNoAODump()
Configure the AnalysisObject dump rate and destination.
Definition: AnalysisHandler.hh:274
AnalysisHandler(const string &runname="")
Preferred constructor, with optional run name.
Stage
Definition: AnalysisHandler.hh:311
void readData(const std::string &filename)
Read analysis plots into the histo collection (via addData) from the named file.
void selectMultiWeights(std::string patterns="")
Setter for _matchWeightNames.
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.
void setNominalWeightName(std::string name="")
Setter for _nominalWeightName.
~AnalysisHandler()
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.
void mergeYodas(const vector< string > &aofiles, const vector< string > &delopts=vector< string >(), const vector< string > &addopts=vector< string >(), bool equiv=false)
Merge the vector of YODA files, using the cross-section and weight information provided in each...
void dump(const string &dumpfile, int period)
Definition: AnalysisHandler.hh:279
This is the base class of all analysis classes in Rivet.
Definition: Analysis.hh:64
size_t numWeights() const
Indices of the weights in the original weight matrix.
Definition: AnalysisHandler.hh:64
AnalysisHandler & removeAnalysis(const std::string &analysisname)
Remove an analysis from the run list using its name.
void checkBeams(bool check=true)
Definition: AnalysisHandler.hh:148
const vector< string > & weightNames() const
Names of event weight categories.
Definition: AnalysisHandler.hh:58
Stage stage() const
Return the current processing stage.
Definition: AnalysisHandler.hh:314
Error relating to looking up analysis objects in the register.
Definition: Exceptions.hh:61
AnalysisHandler & operator=(const AnalysisHandler &)=delete
The assignment operator is deleted, so it can never be called.
void setNLOSmearing(double frac)
Set the relative width of the NLO smearing window.
Definition: AnalysisHandler.hh:79
The key class for coordination of Analysis objects and the event loop.
Definition: AnalysisHandler.hh:23
const std::map< std::string, AnaHandle > & analysesMap() const
Get the collection of currently registered analyses.
Definition: AnalysisHandler.hh:166
double sumW() const
Access the sum of the event weights seen.
Definition: AnalysisHandler.hh:53
const YODA::AnalysisObjectPtr getPreload(string path) const
Definition: AnalysisHandler.hh:256
double sqrtS() const
size_t numEvents() const
string runName() const
Get the name of this run.
void setAODump(const string &dumpfile, int period)
Configure the AnalysisObject dump rate and destination.
Definition: AnalysisHandler.hh:270
void setCrossSection(const pair< double, double > &xsec, bool isUserSupplied=false)
Set the cross-section for the process being generated.
size_t defaultWeightIndex() const
Get the index of the nominal weight-stream.
Definition: AnalysisHandler.hh:73
std::vector< std::string > stdAnalysisNames() const
Get a list of the official analysis names for this release.
void setIgnoreBeams(bool ignore=true)
AnaHandle analysis(const std::string &analysisname)
Get a registered analysis by name.
Definition: AnalysisHandler.hh:179
AnalysisHandler & setRunBeams(const ParticlePair &beams)
Set the beam particles for this run.
Definition: AnalysisHandler.hh:129