File indexing completed on 2026-12-02 10:19:03
0001
0002 #ifndef RIVET_RivetHandler_HH
0003 #define RIVET_RivetHandler_HH
0004
0005 #include "Rivet/AnalysisLoader.hh"
0006 #include "Rivet/Particle.hh"
0007 #include "Rivet/ProjectionHandler.hh"
0008 #include "Rivet/Config/RivetCommon.hh"
0009 #include "Rivet/Tools/RivetYODA.hh"
0010 #include "Rivet/Tools/Utils.hh"
0011 #include "YODA/ReaderYODA.h"
0012
0013 #include <fstream>
0014 #include <unordered_map>
0015
0016 namespace Rivet {
0017
0018
0019
0020 class Analysis;
0021 using AnaHandle = std::shared_ptr<Analysis>;
0022
0023
0024
0025
0026
0027
0028
0029 class AnalysisHandler {
0030
0031 using TypeHandlePtr = std::shared_ptr<TypeBaseHandle>;
0032 using TypeRegister = std::unordered_map<string, TypeHandlePtr>;
0033 using TypeRegisterItr = typename TypeRegister::const_iterator;
0034
0035 public:
0036
0037 using Annotations = std::map<std::string, std::string>;
0038
0039
0040 AnalysisHandler();
0041
0042
0043 AnalysisHandler(const AnalysisHandler&) = delete;
0044
0045
0046 AnalysisHandler& operator=(const AnalysisHandler&) = delete;
0047
0048
0049 ~AnalysisHandler();
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 size_t numEvents() const {
0061 const double N = _eventCounter.get()->persistent(defaultWeightIndex())->numEntries();
0062 return size_t(N + 0.5 - (N < 0));
0063 }
0064
0065
0066
0067
0068
0069
0070 double effNumEvents() const {
0071 if ((bool)_eventCounter) {
0072 return _eventCounter->effNumEntries();
0073 }
0074 return _eventCounter.get()->persistent(defaultWeightIndex())->effNumEntries();
0075 }
0076
0077
0078
0079
0080
0081 double sumW() const {
0082 if ((bool)_eventCounter) {
0083 return _eventCounter->sumW();
0084 }
0085 return _eventCounter.get()->persistent(defaultWeightIndex())->sumW();
0086 }
0087
0088 double sumW2() const {
0089 if ((bool)_eventCounter) {
0090 return _eventCounter->sumW2();
0091 }
0092 return _eventCounter.get()->persistent(defaultWeightIndex())->sumW2();
0093 }
0094
0095
0096
0097
0098
0099
0100
0101
0102 const vector<string>& weightNames() const {
0103 return _weightNames;
0104 }
0105
0106
0107 size_t numWeights() const {
0108 return _weightNames.size();
0109 }
0110
0111
0112 bool haveNamedWeights() const;
0113
0114
0115 void setWeightNames(const GenEvent& ge);
0116
0117
0118 void setWeightNames(const vector<string>& weightNames);
0119
0120
0121 size_t defaultWeightIndex() const {
0122 return _rivetDefaultWeightIdx;
0123 }
0124
0125
0126 vector<double> weightSumWs() const;
0127
0128
0129 void setWeightCap(const double maxWeight) {
0130 _weightCap = maxWeight;
0131 }
0132
0133
0134 void setNominalWeightName(const std::string& name) {
0135 _nominalWeightName = name;
0136 }
0137
0138
0139 void skipMultiWeights(bool skip = false) {
0140 _skipMultiWeights = skip;
0141 }
0142
0143
0144 void matchWeightNames(const std::string& patterns) {
0145 _matchWeightNames = patterns;
0146 }
0147
0148
0149 void unmatchWeightNames(const std::string& patterns) {
0150 _unmatchWeightNames = patterns;
0151 }
0152
0153
0154 void setNLOSmearing(double frac) {
0155 _NLOSmearing = frac;
0156 }
0157
0158
0159
0160
0161
0162
0163
0164
0165 Estimate0DPtr crossSection() const {
0166 return _xs;
0167 }
0168
0169
0170 void setCrossSection(const vector<pair<double, double>>& xsecs, bool isUserSupplied = false);
0171
0172
0173 void setCrossSection(const pair<double, double>& xsec, bool isUserSupplied = false);
0174
0175
0176 void setCrossSection(double xsec, double xsecerr, bool isUserSupplied = false) {
0177 setCrossSection({xsec, xsecerr}, isUserSupplied);
0178 }
0179
0180
0181
0182
0183 void updateCrossSection();
0184
0185
0186 void notifyEndOfFile() {
0187 _isEndOfFile = true;
0188 }
0189
0190
0191 double nominalCrossSection() const;
0192
0193
0194 double nominalCrossSectionError() const;
0195
0196
0197
0198
0199
0200
0201
0202
0203 AnalysisHandler& setRunBeams(const ParticlePair& beams);
0204
0205
0206 const ParticlePair& runBeams() const {
0207 return _beams;
0208 }
0209
0210
0211 PdgIdPair runBeamIDs() const;
0212
0213
0214 pair<double, double> runBeamEnergies() const;
0215
0216
0217 double runSqrtS() const;
0218
0219
0220 void setCheckBeams(bool check = true) {
0221 _checkBeams = check;
0222 }
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 std::vector<std::string> annotations() const {
0237 return _beaminfo->annotations();
0238 }
0239
0240
0241 bool hasAnnotation(const std::string& name) const {
0242 return _beaminfo->hasAnnotation(name);
0243 }
0244
0245
0246 const std::string& annotation(const std::string& name) const {
0247 return _beaminfo->annotation(name);
0248 }
0249
0250
0251 const std::string& annotation(const std::string& name, const std::string& defaultreturn) const {
0252 return _beaminfo->annotation(name, defaultreturn);
0253 }
0254
0255
0256
0257
0258 template <typename T>
0259 const T annotation(const std::string& name) const {
0260 return _beaminfo->annotation<T>(name);
0261 }
0262
0263
0264
0265
0266 template <typename T>
0267 const T annotation(const std::string& name, T&& defaultreturn) const {
0268 return _beaminfo->annotation<T>(name, std::forward<T>(defaultreturn));
0269 }
0270
0271
0272
0273
0274 template <typename T>
0275 void setAnnotation(const std::string& name, T&& value) {
0276 _beaminfo->setAnnotation<T>(name, std::forward<T>(value));
0277 }
0278
0279
0280
0281 void setAnnotations(const Annotations& anns) {
0282 _beaminfo->setAnnotations(anns);
0283 }
0284
0285
0286 void rmAnnotation(const std::string& name) {
0287 _beaminfo->rmAnnotation(name);
0288 }
0289
0290
0291
0292 void clearAnnotations() {
0293 _beaminfo->clearAnnotations();
0294 }
0295
0296
0297
0298
0299
0300
0301
0302
0303 template <typename T>
0304 void registerType() {
0305 const std::string name = T().type();
0306 const TypeRegisterItr& res = _register.find(name);
0307 if (res == _register.end()) {
0308 _register[name] = make_shared<TypeHandle<T>>();
0309 }
0310 _reader.registerType<T>();
0311 }
0312
0313
0314
0315 bool copyAO(YODA::AnalysisObjectPtr src, YODA::AnalysisObjectPtr dst, const double scale = 1.0);
0316
0317
0318
0319 bool addAO(YODA::AnalysisObjectPtr src, YODA::AnalysisObjectPtr& dst, const double scale);
0320
0321
0322
0323
0324
0325
0326
0327
0328 std::vector<std::string> analysisNames() const;
0329
0330
0331 std::vector<std::string> stdAnalysisNames() const;
0332
0333
0334 const std::map<std::string, AnaHandle>& analysesMap() const {
0335 return _analyses;
0336 }
0337
0338
0339 std::vector<AnaHandle> analyses() const {
0340 std::vector<AnaHandle> rtn;
0341 rtn.reserve(_analyses.size());
0342 for (const auto& apair : _analyses) rtn.push_back(apair.second);
0343 return rtn;
0344 }
0345
0346
0347 AnaHandle analysis(const std::string& analysisname) {
0348 if (_analyses.find(analysisname) == _analyses.end())
0349 throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
0350 try {
0351 return _analyses[analysisname];
0352 }
0353 catch (...) {
0354 throw LookupError("No analysis named '" + analysisname + "' registered in AnalysisHandler");
0355 }
0356 }
0357
0358
0359 AnalysisHandler& addAnalysis(Analysis* analysis);
0360
0361
0362
0363
0364
0365
0366 AnalysisHandler& addAnalysis(const std::string& analysisname);
0367
0368
0369
0370
0371 AnalysisHandler& addAnalysis(const std::string& analysisname, std::map<string, string> pars);
0372
0373
0374
0375
0376
0377
0378
0379 AnalysisHandler& addAnalyses(const std::vector<std::string>& analysisnames);
0380
0381
0382
0383 AnalysisHandler& removeAnalysis(const std::string& analysisname);
0384
0385
0386 AnalysisHandler& removeAnalyses(const std::vector<std::string>& analysisnames);
0387
0388
0389
0390
0391
0392
0393
0394
0395 void init(const GenEvent& event);
0396
0397
0398
0399
0400
0401
0402
0403 void analyze(GenEvent& event);
0404
0405
0406
0407
0408
0409 void analyze(GenEvent* event);
0410
0411
0412
0413 void finalize();
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423 void collapseEventGroup();
0424
0425
0426
0427
0428 void readData(std::istream& istr, const string& fmt, bool preload = true);
0429
0430
0431 void readData(const std::string& filename, bool preload = true);
0432
0433
0434
0435
0436 vector<YODA::AnalysisObjectPtr> getYodaAOs(const bool includeraw = false,
0437 const bool mkinert = true) const;
0438
0439
0440 vector<YODA::AnalysisObjectPtr> getRawAOs() const;
0441
0442
0443 vector<std::string> getRawAOPaths() const;
0444
0445
0446
0447 const YODA::AnalysisObjectPtr getPreload(const string& path) const {
0448 auto it = _preloads.find(path);
0449 if (it == _preloads.end()) return nullptr;
0450 return it->second;
0451 }
0452
0453
0454
0455
0456 void writeData(std::ostream& ostr, const string& fmt) const;
0457
0458
0459 void writeData(const string& filename) const;
0460
0461
0462
0463
0464
0465
0466 void setFinalizePeriod(const string& dumpfile, int period) {
0467 _dumpPeriod = period;
0468 _dumpFile = dumpfile;
0469 }
0470
0471 void setNoFinalizePeriod() {
0472 setFinalizePeriod("DUMMY", -1);
0473 }
0474
0475
0476 void setBootstrapFilename(const string& filename) {
0477 _bootstrapfilename = filename;
0478 }
0479
0480
0481 vector<pair<string, size_t>> fillLayout() const;
0482
0483
0484 vector<bool> fillOutcomes() const;
0485
0486
0487 vector<double> fillFractions() const;
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504 void mergeYODAs(const vector<string>& aofiles,
0505 const vector<string>& delopts = vector<string>(),
0506 const vector<string>& addopts = vector<string>(),
0507 const vector<string>& matches = vector<string>(),
0508 const vector<string>& unmatches = vector<string>(),
0509 const bool equiv = false,
0510 const bool reentrantOnly = true,
0511 const bool checkbeams = true);
0512
0513
0514 void merge(AnalysisHandler& other);
0515
0516
0517
0518
0519
0520
0521 void loadAOs(const vector<string>& aoPaths, const vector<double>& aoData);
0522
0523
0524
0525
0526
0527
0528 vector<double> serializeContent(bool fixed_length = false) {
0529 if (!_initialised) throw Error("AnalysisHandler has not been initialised!");
0530
0531 collapseEventGroup();
0532
0533
0534 const vector<YODA::AnalysisObjectPtr> raos = getRawAOs();
0535 size_t total = 0;
0536 for (size_t i = 0; i < raos.size(); ++i) {
0537 total += raos[i]->lengthContent(fixed_length) + 1;
0538 }
0539 total += _beaminfo->numBins() + 1;
0540
0541
0542 std::vector<double> data;
0543 data.reserve(total);
0544
0545 data.push_back(_beaminfo->numBins());
0546 for (const string& beamID : _beaminfo->xEdges()) {
0547 data.push_back(_beamInfoLabelToID(beamID));
0548 }
0549
0550 for (size_t i = 0; i < raos.size(); ++i) {
0551 vector<double> tmp = raos[i]->serializeContent(fixed_length);
0552 data.push_back(tmp.size());
0553 data.insert(std::end(data), std::make_move_iterator(std::begin(tmp)),
0554 std::make_move_iterator(std::end(tmp)));
0555 }
0556 return data;
0557 }
0558
0559 void deserializeContent(const vector<double>& data, size_t nprocs = 0) {
0560 if (!_initialised) throw Error("AnalysisHandler has not been initialised!");
0561
0562 collapseEventGroup();
0563
0564
0565 vector<MultiplexAOPtr> raos = getRivetAOs();
0566
0567
0568
0569 size_t iAO = 0, iW = 0, nBeams = data[0], offset = 1;
0570 if (nprocs) nBeams /= nprocs;
0571 const auto itr = data.cbegin();
0572
0573 vector<int> edges{itr + offset, itr + offset + nBeams};
0574 vector<string> labels;
0575 labels.reserve(edges.size());
0576 size_t id = 0;
0577 for (int edge : edges) {
0578 if (nprocs >= 2) edge /= nprocs;
0579 labels.push_back(_mkBeamInfoLabel(++id, edge));
0580 }
0581
0582 _beaminfo = make_shared<YODA::BinnedEstimate<string>>(labels, "/TMP/_BEAMPZ");
0583 offset += nBeams;
0584
0585 size_t beamLen = *(itr + offset);
0586 ++offset;
0587 if (nprocs) beamLen /= nprocs;
0588 std::vector<double> energies{itr + offset, itr + offset + beamLen};
0589 if (nprocs >= 2) {
0590 for (double& e : energies) {
0591 e /= nprocs;
0592 }
0593 }
0594 _beaminfo->deserializeContent(energies);
0595 for (auto& b : _beaminfo->bins(true)) b.rmErrs();
0596 offset += beamLen;
0597
0598
0599 while (offset < data.size()) {
0600 if (iW < numWeights())
0601 raos[iAO].get()->setActiveWeightIdx(iW);
0602 else {
0603 raos[iAO].get()->unsetActiveWeight();
0604 iW = 0;
0605 ++iAO;
0606 raos[iAO].get()->setActiveWeightIdx(iW);
0607 }
0608
0609
0610 size_t aoLen = *(itr + offset);
0611 ++offset;
0612 if (nprocs) aoLen /= nprocs;
0613 auto first = itr + offset;
0614 auto last = first + aoLen;
0615
0616 raos[iAO].get()->activeAO()->deserializeContent(std::vector<double>{first, last});
0617
0618 ++iW;
0619 offset += aoLen;
0620 }
0621 raos[iAO].get()->unsetActiveWeight();
0622
0623 _ntrials = 0.0;
0624 _fileCounter = CounterPtr(weightNames(), Counter("_FILECOUNT"));
0625 _xserr = CounterPtr(weightNames(), Counter("XSECERR"));
0626 if (nprocs >= 2) {
0627 for (size_t iW = 0; iW < numWeights(); ++iW) {
0628 *_fileCounter.get()->persistent(iW) = *_eventCounter.get()->persistent(iW);
0629 _xs.get()->persistent(iW)->scale(1.0 / nprocs);
0630 }
0631 }
0632 }
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642 enum class Stage { OTHER, INIT, FINALIZE };
0643
0644
0645 Stage stage() const {
0646 return _stage;
0647 }
0648
0649
0650
0651 private:
0652
0653
0654
0655
0656
0657 Log& getLog() const;
0658
0659
0660 vector<MultiplexAOPtr> getRivetAOs() const;
0661
0662
0663 void stripOptions(YODA::AnalysisObjectPtr ao, const vector<string>& delopts) const;
0664
0665
0666 void mergeAOS(map<string, YODA::AnalysisObjectPtr>& allaos,
0667 const map<string, YODA::AnalysisObjectPtr>& newaos,
0668 map<string, std::array<double, 4>>& allxsecs,
0669 const vector<string>& delopts = vector<string>(),
0670 const vector<string>& optAnas = vector<string>(),
0671 const vector<string>& optKeys = vector<string>(),
0672 const vector<string>& optVals = vector<string>(),
0673 const bool equiv = false,
0674 const bool overwrite_xsec = false,
0675 const double user_xsec = 1.0);
0676
0677
0678
0679
0680
0681
0682 void loadAOs(const map<string, YODA::AnalysisObjectPtr>& allAOs,
0683 const bool unscale = false,
0684 const bool reentrantOnly = true);
0685
0686
0687 void _setRunBeamInfo(const ParticlePair& beams);
0688
0689
0690 string _mkBeamInfoLabel(size_t n, PdgId id) {
0691 return "BEAM" + std::to_string(n) + "(" + std::to_string(id) + ")";
0692 }
0693
0694
0695 PdgId _beamInfoLabelToID(const string& label) {
0696 size_t pos = label.find("(");
0697 string beamID = label.substr(pos + 1, label.size() - pos - 2);
0698 return std::stoi(beamID);
0699 }
0700
0701
0702
0703
0704
0705 private:
0706
0707
0708 Stage _stage = Stage::OTHER;
0709
0710
0711 std::map<std::string, AnaHandle> _analyses;
0712
0713
0714
0715
0716 map<string, YODA::AnalysisObjectPtr> _preloads;
0717
0718
0719 vector<YODA::AnalysisObjectPtr> _finalizedAOs;
0720
0721
0722 template <typename... Args>
0723 void registerDefaultTypes();
0724
0725
0726 TypeRegister _register;
0727
0728
0729 YODA::Reader& _reader = YODA::ReaderYODA::create();
0730
0731
0732
0733
0734
0735
0736 std::vector<std::string> _weightNames;
0737 std::vector<std::valarray<double>> _subEventWeights;
0738
0739
0740
0741 std::vector<size_t> _weightIndices;
0742
0743
0744 CounterPtr _eventCounter;
0745
0746
0747 Estimate0DPtr _xs;
0748
0749
0750 CounterPtr _xserr;
0751
0752
0753 YODA::BinnedEstimatePtr<string> _beaminfo;
0754
0755
0756 double _ntrials;
0757
0758
0759 CounterPtr _fileCounter;
0760
0761
0762 bool _isEndOfFile;
0763
0764
0765 bool _subeventWarning;
0766
0767
0768 std::pair<double, double> _userxs;
0769
0770
0771 ParticlePair _beams;
0772
0773
0774 bool _initialised;
0775
0776
0777 bool _checkBeams{true};
0778
0779
0780
0781
0782 bool _ignoreEventGroups{false};
0783
0784
0785 bool _skipMultiWeights{false};
0786
0787
0788 std::string _matchWeightNames;
0789
0790
0791 std::string _unmatchWeightNames;
0792
0793
0794 std::string _nominalWeightName;
0795
0796
0797 double _weightCap;
0798
0799
0800
0801
0802 double _NLOSmearing;
0803
0804
0805 int _eventNumber;
0806
0807
0808 size_t _defaultWeightIdx;
0809
0810
0811 size_t _rivetDefaultWeightIdx;
0812
0813
0814 int _customDefaultWeightIdx;
0815
0816
0817 int _dumpPeriod;
0818
0819
0820 string _dumpFile;
0821
0822
0823 bool _dumping;
0824
0825
0826 ofstream _fbootstrap;
0827
0828
0829 std::string _bootstrapfilename;
0830
0831
0832 ProjectionHandler _projHandler;
0833
0834
0835 };
0836
0837
0838 }
0839
0840 #endif