Rivet  3.1.2
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  const vector<size_t> weightIndices() const { return _weightIndices; }
70 
72  size_t numWeights() const { return _weightNames.size(); }
73 
75  bool haveNamedWeights() const;
76 
78  void setWeightNames(const GenEvent& ge);
79 
81  size_t defaultWeightIndex() const { return _rivetDefaultWeightIdx; }
82 
84  size_t globalDefaultWeightIndex() const { return _defaultWeightIdx; }
85 
87  void setWeightCap(const double maxWeight) { _weightCap = maxWeight; }
88 
90  void setNLOSmearing(double frac) { _NLOSmearing = frac; }
91 
93 
94 
96 
97 
99  Scatter1DPtr crossSection() const { return _xs; }
100 
102  void setCrossSection(pair<double, double> xsec, bool isUserSupplied=false);
104  void setCrossSection(double xsec, double xsecerr, bool isUserSupplied=false) {
105  setCrossSection({xsec, xsecerr}, isUserSupplied);
106  }
107 
109  double nominalCrossSection() const {
110  _xs.get()->setActiveWeightIdx(_rivetDefaultWeightIdx);
111  const YODA::Scatter1D::Points& ps = _xs->points();
112  if (ps.size() != 1) {
113  string errMsg = "cross section missing when requesting nominal cross section";
114  throw Error(errMsg);
115  }
116  double xs = ps[0].x();
117  _xs.get()->unsetActiveWeight();
118  return xs;
119  }
120 
122 
123 
125 
126 
128  AnalysisHandler& setRunBeams(const ParticlePair& beams) {
129  _beams = beams;
130  MSG_DEBUG("Setting run beams = " << beams << " @ " << sqrtS()/GeV << " GeV");
131  return *this;
132  }
133 
135  const ParticlePair& beams() const { return _beams; }
136 
139  PdgIdPair beamIds() const;
140 
143  double sqrtS() const;
144 
146  void setIgnoreBeams(bool ignore=true);
147 
149  void skipMultiWeights(bool ignore=false);
150 
152  void selectMultiWeights(std::string patterns="");
153 
155  void deselectMultiWeights(std::string patterns="");
156 
158 
159 
161 
162 
164  std::vector<std::string> analysisNames() const;
165 
167  std::vector<std::string> stdAnalysisNames() const;
168 
170  const std::map<std::string, AnaHandle>& analysesMap() const {
171  return _analyses;
172  }
173 
175  std::vector<AnaHandle> analyses() const {
176  std::vector<AnaHandle> rtn;
177  rtn.reserve(_analyses.size());
178  for (const auto& apair : _analyses) rtn.push_back(apair.second);
179  return rtn;
180  }
181 
183  AnaHandle analysis(const std::string& analysisname) {
184  if ( _analyses.find(analysisname) == _analyses.end() )
185  throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
186  try {
187  return _analyses[analysisname];
188  } catch (...) {
189  throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
190  }
191  }
192 
195 
201  AnalysisHandler& addAnalysis(const std::string& analysisname);
202 
204  AnalysisHandler& addAnalysis(const std::string& analysisname, std::map<string, string> pars);
205 
212  AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
213 
214 
216  AnalysisHandler& removeAnalysis(const std::string& analysisname);
217 
219  AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
220 
222 
224 
225 
227 
228 
230  void init(const GenEvent& event);
231 
236  void analyze(const GenEvent& event);
237 
242  void analyze(const GenEvent* event);
243 
246  void finalize();
247 
249 
250 
252 
253 
256  void pushToPersistent();
257 
259  void readData(const std::string& filename);
260 
262  vector<YODA::AnalysisObjectPtr> getYodaAOs(bool includeraw=false) const;
263 
266  const YODA::AnalysisObjectPtr getPreload(string path) const {
267  auto it = _preloads.find(path);
268  if ( it == _preloads.end() ) return nullptr;
269  return it->second;
270  }
271 
273  void writeData(const std::string& filename) const;
274 
278  void dump(string dumpfile, int period) {
279  _dumpPeriod = period;
280  _dumpFile = dumpfile;
281  }
282 
297  void mergeYodas(const vector<string> & aofiles,
298  const vector<string> & delopts = vector<string>(),
299  const vector<string> & addopts = vector<string>(),
300  bool equiv = false);
301 
303  void stripOptions(YODA::AnalysisObjectPtr ao,
304  const vector<string> & delopts) const;
305 
307 
308 
311  enum class Stage { OTHER, INIT, FINALIZE };
312 
314  Stage stage() const { return _stage; }
315 
316  protected:
317 
319  vector<MultiweightAOPtr> getRivetAOs() const;
320 
321  std::valarray<double> pruneWeights(const std::valarray<double>& weights);
322 
323 
324  private:
325 
327  Stage _stage = Stage::OTHER;
328 
330  std::map<std::string, AnaHandle> _analyses;
331 
334  map<string,YODA::AnalysisObjectPtr> _preloads;
335 
338  vector<YODA::AnalysisObjectPtr> _finalizedAOs;
339 
340 
342 
343 
345  std::vector<std::string> _weightNames;
346  std::vector<std::valarray<double> > _subEventWeights;
347  //size_t _numWeightTypes; // always == WeightVector.size()
348 
350  std::vector<size_t> _weightIndices;
351 
353  std::string _runname;
354 
356  mutable CounterPtr _eventCounter;
357 
359  Scatter1DPtr _xs;
360 
362  std::pair<double,double> _userxs;
363 
365  ParticlePair _beams;
366 
368  bool _initialised;
369 
371  bool _ignoreBeams;
372 
374  bool _skipWeights;
375 
377  std::string _matchWeightNames;
378 
380  std::string _unmatchWeightNames;
381 
383  double _weightCap;
384 
386  double _NLOSmearing;
387 
389  int _eventNumber;
390 
392  size_t _defaultWeightIdx;
393 
395  size_t _rivetDefaultWeightIdx;
396 
399  int _dumpPeriod;
400 
403  string _dumpFile;
404 
406  bool _dumping;
407 
409 
410 
411  private:
412 
415  AnalysisHandler& operator=(const AnalysisHandler&);
416 
420 
421  };
422 
423 
424 }
425 
426 #endif
Definition: MC_Cent_pPb.hh:10
void deselectMultiWeights(std::string patterns="")
Setter for _unmatchWeightNames.
void setCrossSection(pair< double, double > xsec, bool isUserSupplied=false)
Set the cross-section for the process being generated.
const ParticlePair & beams() const
Get the beam particles for this run, usually determined from the first event.
Definition: AnalysisHandler.hh:135
double nominalCrossSection() const
Get the nominal cross-section.
Definition: AnalysisHandler.hh:109
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 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:87
void skipMultiWeights(bool ignore=false)
Setter for _skipWeights.
std::vector< AnaHandle > analyses() const
Get the collection of currently registered analyses.
Definition: AnalysisHandler.hh:175
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:104
Scatter1DPtr crossSection() const
Get the cross-section known to the handler.
Definition: AnalysisHandler.hh:99
PdgIdPair beamIds() const
#define MSG_DEBUG(x)
Debug messaging, not enabled by default, using MSG_LVL.
Definition: Logging.hh:195
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.
~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.
void mergeYodas(const vector< string > &aofiles, const vector< string > &delopts=vector< string >(), const vector< string > &addopts=vector< string >(), bool equiv=false)
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:72
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:314
Error relating to looking up analysis objects in the register.
Definition: Exceptions.hh:61
void setNLOSmearing(double frac)
Set the relative width of the NLO smearing window.
Definition: AnalysisHandler.hh:90
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:278
const std::map< std::string, AnaHandle > & analysesMap() const
Get the collection of currently registered analyses.
Definition: AnalysisHandler.hh:170
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:266
size_t globalDefaultWeightIndex() const
Get the index of the nominal weight-stream in the original weight matrix.
Definition: AnalysisHandler.hh:84
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.
const vector< size_t > weightIndices() const
Indices of the weights in the original weight matrix.
Definition: AnalysisHandler.hh:69
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:81
std::vector< std::string > stdAnalysisNames() const
Get a list of the official analysis names for this release.
void setIgnoreBeams(bool ignore=true)
Setter for _ignoreBeams.
AnaHandle analysis(const std::string &analysisname)
Get a registered analysis by name.
Definition: AnalysisHandler.hh:183
AnalysisHandler & setRunBeams(const ParticlePair &beams)
Set the beam particles for this run.
Definition: AnalysisHandler.hh:128