rivet is hosted by Hepforge, IPPP Durham
Rivet 3.1.6
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
10namespace 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
31
34
37
38
41
43 string runName() const;
44
50 size_t numEvents() const {
51 const double N = _eventCounter.get()->_getPersistent(defaultWeightIndex())->numEntries();
52 return size_t(N + 0.5 - (N<0)); // round to nearest integer
53 }
54
59 double sumW() const { return _eventCounter->sumW(); }
61 double sumW2() const { return _eventCounter->sumW2(); }
62
64 const vector<string>& weightNames() const { return _weightNames; }
65
67 //const vector<size_t> weightIndices() const { return _weightIndices; }
68
70 size_t numWeights() const { return _weightNames.size(); }
71
73 bool haveNamedWeights() const;
74
76 void setWeightNames(const GenEvent& ge);
77
79 size_t defaultWeightIndex() const { return _rivetDefaultWeightIdx; }
80
82 void setWeightCap(const double maxWeight) { _weightCap = maxWeight; }
83
85 void setNLOSmearing(double frac) { _NLOSmearing = frac; }
86
88 void skipMultiWeights(bool ignore=false);
89
91 void selectMultiWeights(std::string patterns="");
92
94 void deselectMultiWeights(std::string patterns="");
95
97 void setNominalWeightName(std::string name="");
98
100
101
104
106 Scatter1DPtr crossSection() const { return _xs; }
107
109 void setCrossSection(const vector<pair<double,double>>& xsecs, bool isUserSupplied = false);
110
112 void setCrossSection(const pair<double, double>& xsec, bool isUserSupplied=false);
113
115 void setCrossSection(double xsec, double xsecerr, bool isUserSupplied=false) {
116 setCrossSection({xsec, xsecerr}, isUserSupplied);
117 }
118
120 double nominalCrossSection() const {
121 _xs.get()->setActiveWeightIdx(_rivetDefaultWeightIdx);
122 const YODA::Scatter1D::Points& ps = _xs->points();
123 if (ps.size() != 1) {
124 string errMsg = "value missing when requesting nominal cross-section";
125 throw Error(errMsg);
126 }
127 double xs = ps[0].x();
128 _xs.get()->unsetActiveWeight();
129 return xs;
130 }
131
133
134
137
140 _beams = beams;
141 MSG_DEBUG("Setting run beams = " << beams << " @ " << sqrtS()/GeV << " GeV");
142 return *this;
143 }
144
146 const ParticlePair& beams() const { return _beams; }
147
150 PdgIdPair beamIds() const;
151
154 double sqrtS() const;
155
157 void checkBeams(bool check=true) { setIgnoreBeams(!check); }
160 void setIgnoreBeams(bool ignore=true);
161
163
164
167
169 std::vector<std::string> analysisNames() const;
170
172 std::vector<std::string> stdAnalysisNames() const;
173
175 const std::map<std::string, AnaHandle>& analysesMap() const {
176 return _analyses;
177 }
178
180 std::vector<AnaHandle> analyses() const {
181 std::vector<AnaHandle> rtn;
182 rtn.reserve(_analyses.size());
183 for (const auto& apair : _analyses) rtn.push_back(apair.second);
184 return rtn;
185 }
186
188 AnaHandle analysis(const std::string& analysisname) {
189 if ( _analyses.find(analysisname) == _analyses.end() )
190 throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
191 try {
192 return _analyses[analysisname];
193 } catch (...) {
194 throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
195 }
196 }
197
200
206 AnalysisHandler& addAnalysis(const std::string& analysisname);
207
209 AnalysisHandler& addAnalysis(const std::string& analysisname, std::map<string, string> pars);
210
217 AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
218
219
221 AnalysisHandler& removeAnalysis(const std::string& analysisname);
222
224 AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
225
227
228
231
233 void init(const GenEvent& event);
234
239 void analyze(const GenEvent& event);
240
245 void analyze(const GenEvent* event);
246
249 void finalize();
250
252
253
256
260 void readData(std::istream& istr, const string& fmt, bool preload = true);
261
263 void readData(const std::string& filename, bool preload = true);
264
266 vector<YODA::AnalysisObjectPtr> getYodaAOs(bool includeraw=false) const;
267
270 const YODA::AnalysisObjectPtr getPreload(string path) const {
271 auto it = _preloads.find(path);
272 if ( it == _preloads.end() ) return nullptr;
273 return it->second;
274 }
275
279 void writeData(std::ostream& ostr, const string& fmt) const;
280
282 void writeData(const string& filename) const;
283
289 void setAODump(const string& dumpfile, int period) {
290 dump(dumpfile, period);
291 }
293 void setNoAODump() {
294 setAODump("DUMMY", -1);
295 }
298 void dump(const string& dumpfile, int period) {
299 _dumpPeriod = period;
300 _dumpFile = dumpfile;
301 }
302
317
318 void mergeYodas(const vector<string>& aofiles,
319 const vector<string>& delopts=vector<string>(),
320 const vector<string>& addopts=vector<string>(),
321 const vector<string>& matches=vector<string>(),
322 const vector<string>& unmatches=vector<string>(),
323 bool equiv=false);
324
326 void merge(AnalysisHandler &other);
327
329
330
333
336 enum class Stage { OTHER, INIT, FINALIZE };
337
339 Stage stage() const { return _stage; }
340
342
343
344 private:
345
348
350 Log& getLog() const;
351
353 vector<MultiweightAOPtr> getRivetAOs() const;
354
356 void stripOptions(YODA::AnalysisObjectPtr ao, const vector<string>& delopts) const;
357
360 void pushToPersistent();
361
363 void mergeAOS(map<string, YODA::AnalysisObjectPtr> &allaos,
364 map<string, YODA::AnalysisObject*> &newaos,
365 map<string, pair<double, double>> &allxsecs,
366 const vector<string>& delopts=vector<string>(),
367 const vector<string>& optAnas=vector<string>(),
368 const vector<string>& optKeys=vector<string>(),
369 const vector<string>& optVals=vector<string>(),
370 bool equiv=false,
371 const bool overwrite_xsec = false,
372 const double user_xsec = 1.0);
373
374
379 void loadAOs(const map<string, YODA::AnalysisObjectPtr>& allAOs, const bool unscale = false);
380
382
383
384 private:
385
387 Stage _stage = Stage::OTHER;
388
390 std::map<std::string, AnaHandle> _analyses;
391
395 map<string,YODA::AnalysisObjectPtr> _preloads;
396
398 vector<YODA::AnalysisObjectPtr> _finalizedAOs;
399
400
403
405 std::vector<std::string> _weightNames;
406 std::vector<std::valarray<double> > _subEventWeights;
407 //size_t _numWeightTypes; // always == WeightVector.size()
408
410 std::vector<size_t> _weightIndices;
411
413 std::string _runname;
414
416 CounterPtr _eventCounter;
417
419 Scatter1DPtr _xs;
420
422 std::pair<double,double> _userxs;
423
425 ParticlePair _beams;
426
428 bool _initialised;
429
431 bool _ignoreBeams;
432
434 bool _skipWeights;
435
437 std::string _matchWeightNames;
438
440 std::string _unmatchWeightNames;
441
443 std::string _nominalWeightName;
444
446 double _weightCap;
447
451 double _NLOSmearing;
452
454 int _eventNumber;
455
457 size_t _defaultWeightIdx;
458
460 size_t _rivetDefaultWeightIdx;
461
463 int _dumpPeriod;
464
466 string _dumpFile;
467
469 bool _dumping;
470
472
473 };
474
475
476}
477
478#endif
The key class for coordination of Analysis objects and the event loop.
Definition: AnalysisHandler.hh:23
AnalysisHandler & addAnalysis(const std::string &analysisname, std::map< string, string > pars)
Add an analysis with a map of analysis options.
void setNLOSmearing(double frac)
Set the relative width of the NLO smearing window.
Definition: AnalysisHandler.hh:85
std::vector< std::string > stdAnalysisNames() const
Get a list of the official analysis names for this release.
PdgIdPair beamIds() const
void setAODump(const string &dumpfile, int period)
Configure the AnalysisObject dump rate and destination.
Definition: AnalysisHandler.hh:289
size_t defaultWeightIndex() const
Get the index of the nominal weight-stream.
Definition: AnalysisHandler.hh:79
void writeData(const string &filename) const
Write all analyses' plots (via getData) to the named file.
AnalysisHandler & setRunBeams(const ParticlePair &beams)
Set the beam particles for this run.
Definition: AnalysisHandler.hh:139
void setCrossSection(double xsec, double xsecerr, bool isUserSupplied=false)
Set the cross-section for the process being generated (alternative signature)
Definition: AnalysisHandler.hh:115
void init(const GenEvent &event)
Initialize a run, with the run beams taken from the example event.
AnalysisHandler & addAnalyses(const std::vector< std::string > &analysisnames)
Add analyses to the run list using their names.
AnalysisHandler & removeAnalyses(const std::vector< std::string > &analysisnames)
Remove analyses from the run list using their names.
const ParticlePair & beams() const
Get the beam particles for this run, usually determined from the first event.
Definition: AnalysisHandler.hh:146
size_t numWeights() const
Indices of the weights in the original weight matrix.
Definition: AnalysisHandler.hh:70
void selectMultiWeights(std::string patterns="")
Setter for _matchWeightNames.
void setNominalWeightName(std::string name="")
Setter for _nominalWeightName.
void setNoAODump()
Configure the AnalysisObject dump rate and destination.
Definition: AnalysisHandler.hh:293
void setCrossSection(const pair< double, double > &xsec, bool isUserSupplied=false)
Set all cross-sections for the process being generated, based on nominal weight.
string runName() const
Get the name of this run.
AnalysisHandler(const string &runname="")
Preferred constructor, with optional run name.
void dump(const string &dumpfile, int period)
Definition: AnalysisHandler.hh:298
~AnalysisHandler()
The destructor is not virtual, as this class should not be inherited from.
void merge(AnalysisHandler &other)
A method to merge another AnalysisHandler into the current one.
void readData(const std::string &filename, bool preload=true)
Read analysis plots into the histo collection (via addData) from the named file.
void analyze(const GenEvent *event)
Analyze the given event by pointer.
double sqrtS() const
void mergeYodas(const vector< string > &aofiles, const vector< string > &delopts=vector< string >(), const vector< string > &addopts=vector< string >(), const vector< string > &matches=vector< string >(), const vector< string > &unmatches=vector< string >(), bool equiv=false)
Merge the vector of YODA files, using the cross-section and weight information provided in each.
const vector< string > & weightNames() const
Names of event weight categories.
Definition: AnalysisHandler.hh:64
void setCrossSection(const vector< pair< double, double > > &xsecs, bool isUserSupplied=false)
Set all cross-sections for the process being generated specifically (preferred)
void readData(std::istream &istr, const string &fmt, bool preload=true)
Read analysis plots into the histo collection from the given stream.
Stage stage() const
Return the current processing stage.
Definition: AnalysisHandler.hh:339
double sumW2() const
Access to the sum of squared-weights.
Definition: AnalysisHandler.hh:61
AnalysisHandler & addAnalysis(Analysis *analysis)
Add an analysis to the run list by object.
void setIgnoreBeams(bool ignore=true)
bool haveNamedWeights() const
Are any of the weights non-numeric?
void setWeightNames(const GenEvent &ge)
Set the weight names from a GenEvent.
size_t numEvents() const
Definition: AnalysisHandler.hh:50
std::vector< AnaHandle > analyses() const
Get the collection of currently registered analyses.
Definition: AnalysisHandler.hh:180
void deselectMultiWeights(std::string patterns="")
Setter for _unmatchWeightNames.
void setWeightCap(const double maxWeight)
Set the weight cap.
Definition: AnalysisHandler.hh:82
AnaHandle analysis(const std::string &analysisname)
Get a registered analysis by name.
Definition: AnalysisHandler.hh:188
const YODA::AnalysisObjectPtr getPreload(string path) const
Definition: AnalysisHandler.hh:270
void writeData(std::ostream &ostr, const string &fmt) const
Write all analyses' plots (via getData) to the given stream.
const std::map< std::string, AnaHandle > & analysesMap() const
Get the collection of currently registered analyses.
Definition: AnalysisHandler.hh:175
AnalysisHandler & addAnalysis(const std::string &analysisname)
Add an analysis to the run list using its name.
Stage
Definition: AnalysisHandler.hh:336
vector< YODA::AnalysisObjectPtr > getYodaAOs(bool includeraw=false) const
Get all YODA analysis objects (across all weights, optionally including RAW)
AnalysisHandler & removeAnalysis(const std::string &analysisname)
Remove an analysis from the run list using its name.
AnalysisHandler(const AnalysisHandler &)=delete
The copy constructor is deleted, so it can never be called.
void checkBeams(bool check=true)
Option to disable AH beam-consistency checks.
Definition: AnalysisHandler.hh:157
void skipMultiWeights(bool ignore=false)
Setter for _skipWeights.
double nominalCrossSection() const
Get the nominal cross-section.
Definition: AnalysisHandler.hh:120
double sumW() const
Access the sum of the event weights seen.
Definition: AnalysisHandler.hh:59
Scatter1DPtr crossSection() const
Get the cross-section known to the handler.
Definition: AnalysisHandler.hh:106
AnalysisHandler & operator=(const AnalysisHandler &)=delete
The assignment operator is deleted, so it can never be called.
void analyze(const GenEvent &event)
Analyze the given event by reference.
std::vector< std::string > analysisNames() const
Get a list of the currently registered analyses' names.
This is the base class of all analysis classes in Rivet.
Definition: Analysis.hh:64
Logging system for controlled & formatted writing to stdout.
Definition: Logging.hh:10
#define MSG_DEBUG(x)
Debug messaging, not enabled by default, using MSG_LVL.
Definition: Logging.hh:195
Definition: MC_Cent_pPb.hh:10
std::pair< Particle, Particle > ParticlePair
Typedef for a pair of Particle objects.
Definition: Particle.hh:42
Generic runtime Rivet error.
Definition: Exceptions.hh:12
Error relating to looking up analysis objects in the register.
Definition: Exceptions.hh:61