hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
Loading...
Searching...
No Matches
RunScaleCorrectionTool.cxx
Go to the documentation of this file.
2
3#include <algorithm>
4#include <fstream>
5#include <iostream>
6#include <stdexcept>
7#include <vector>
8
9#include "json.hpp"
10using json = nlohmann::json;
11
12namespace {
13json readJson(const std::string& file) {
14 std::ifstream ifs(file);
15 if (!ifs.good()) throw std::runtime_error("[RunScaleCorrectionTool] cannot open " + file);
16 json cfg;
17 ifs >> cfg;
18 return cfg;
19}
20
21// Same lookup as TrackSmearingTool::lookupBinnedValue: clamp to the first/last bin.
22double lookupBinned(const std::vector<double>& edges, const std::vector<double>& values, double x) {
23 if (edges.size() < 2 || values.empty()) return 0.;
24 if (x <= edges.front()) return values.front();
25 if (x >= edges.back()) return values.back();
26 int bin = static_cast<int>(std::distance(edges.begin(), std::upper_bound(edges.begin(), edges.end(), x))) - 1;
27 bin = std::max(0, std::min(bin, static_cast<int>(values.size()) - 1));
28 return values[bin];
29}
30} // namespace
31
32RunScaleCorrectionTool::RunScaleCorrectionTool(const std::string& runScaleCfg, const std::string& meanCorrCfg,
33 const std::string& meanCorrVariable)
34 : runScaleCfg_(runScaleCfg), meanCorrCfg_(meanCorrCfg), meanCorrVariable_(meanCorrVariable) {
35 if (!runScaleCfg_.empty()) {
36 json cfg = readJson(runScaleCfg_);
37 if (!cfg.contains("runScale"))
38 throw std::runtime_error("[RunScaleCorrectionTool] no 'runScale' block in " + runScaleCfg_);
39 const json& rs = cfg["runScale"];
40 if (rs.contains("default")) {
41 defaultTop_ = rs["default"].value("top", 1.0);
42 defaultBot_ = rs["default"].value("bot", 1.0);
43 }
44 if (rs.contains("runs")) {
45 for (auto it = rs["runs"].begin(); it != rs["runs"].end(); ++it) {
46 const json& entry = it.value();
47 if (!entry.contains("top") || !entry.contains("bot"))
48 throw std::runtime_error("[RunScaleCorrectionTool] run " + it.key() + " lacks a top/bot scale in " +
49 runScaleCfg_);
50 scales_[std::stoi(it.key())] = {entry["top"].get<double>(), entry["bot"].get<double>()};
51 }
52 }
53 }
54
55 if (!meanCorrCfg_.empty()) {
56 json cfg = readJson(meanCorrCfg_);
57 if (!cfg.contains("omegaSmearing") || !cfg["omegaSmearing"].contains("mu_data") ||
58 !cfg["omegaSmearing"].contains("mu_mc"))
59 throw std::runtime_error("[RunScaleCorrectionTool] no omegaSmearing.mu_data / mu_mc in " + meanCorrCfg_);
60 const json& os = cfg["omegaSmearing"];
61 omegaMuDataTop_ = os["mu_data"]["top"].get<double>();
62 omegaMuDataBot_ = os["mu_data"]["bot"].get<double>();
63 omegaMuMcTop_ = os["mu_mc"]["top"].get<double>();
64 omegaMuMcBot_ = os["mu_mc"]["bot"].get<double>();
65
66 if (!meanCorrVariable_.empty()) {
67 const std::string key = "omegaSmearing_binned_" + meanCorrVariable_;
68 if (cfg.contains(key) && cfg[key]["top"].contains("mu_data") && cfg[key]["top"].contains("mu_mc")) {
69 const json& b = cfg[key];
70 edgesTop_ = b["top"]["bin_edges"].get<std::vector<double>>();
71 muDataTop_ = b["top"]["mu_data"].get<std::vector<double>>();
72 muMcTop_ = b["top"]["mu_mc"].get<std::vector<double>>();
73 edgesBot_ = b["bot"]["bin_edges"].get<std::vector<double>>();
74 muDataBot_ = b["bot"]["mu_data"].get<std::vector<double>>();
75 muMcBot_ = b["bot"]["mu_mc"].get<std::vector<double>>();
76 binnedMeans_ = true;
77 } else {
78 std::cout << "[RunScaleCorrectionTool] WARNING: no binned means in " << key
79 << "; using the unbinned omegaSmearing means" << std::endl;
80 }
81 }
82 }
83}
84
85double RunScaleCorrectionTool::lookupValue(const Track& track) const {
86 if (meanCorrVariable_ == "phi0" || meanCorrVariable_ == "phi") return track.getPhi();
87 if (meanCorrVariable_ == "nHits" || meanCorrVariable_ == "nhits") return track.getTrackerHitCount();
88 return track.getTanLambda();
89}
90
91double RunScaleCorrectionTool::omegaMeanShift(const Track& track, bool isTop) const {
92 if (binnedMeans_) {
93 const double x = lookupValue(track);
94 return isTop ? lookupBinned(edgesTop_, muMcTop_, x) - lookupBinned(edgesTop_, muDataTop_, x)
95 : lookupBinned(edgesBot_, muMcBot_, x) - lookupBinned(edgesBot_, muDataBot_, x);
96 }
98}
99
100double RunScaleCorrectionTool::getScale(int run, bool isTop) const {
101 if (runScaleCfg_.empty()) return 1.0;
102 auto it = scales_.find(run);
103 if (it != scales_.end()) return isTop ? it->second.first : it->second.second;
104 if (warnedRuns_.insert(run).second)
105 std::cout << "[RunScaleCorrectionTool] WARNING: run " << run << " not in " << runScaleCfg_
106 << "; using the default scale (top " << defaultTop_ << ", bot " << defaultBot_ << ")" << std::endl;
107 return isTop ? defaultTop_ : defaultBot_;
108}
109
110double RunScaleCorrectionTool::apply(Track& track, int run) const {
111 const bool isTop = track.getTanLambda() > 0;
112 const double omega = track.getOmega();
113 double scale = getScale(run, isTop);
114 double omegaCorr = omega / scale;
115
116 if (!meanCorrCfg_.empty()) {
117 // Same as TrackSmearingTool's data-mode omega mean correction
118 omegaCorr += omegaMeanShift(track, isTop);
119 if (omegaCorr != 0.) scale = omega / omegaCorr;
120 }
121
122 if (scale == 1.0) return scale;
123 std::vector<double> mom = track.getMomentum();
124 for (double& c : mom) c *= scale;
125 track.setMomentum(mom);
126 track.setOmega(omega / scale);
127 return scale;
128}
129
131 if (runScaleCfg_.empty())
132 std::cout << "[RunScaleCorrectionTool] run-by-run scale: off" << std::endl;
133 else
134 std::cout << "[RunScaleCorrectionTool] run-by-run scale: " << runScaleCfg_ << " (" << scales_.size()
135 << " runs, default top " << defaultTop_ << " bot " << defaultBot_ << ")" << std::endl;
136 if (meanCorrCfg_.empty())
137 std::cout << "[RunScaleCorrectionTool] mean correction: off" << std::endl;
138 else
139 std::cout << "[RunScaleCorrectionTool] mean correction: " << meanCorrCfg_
140 << (binnedMeans_ ? " (binned in " + meanCorrVariable_ + ")" : " (unbinned)") << std::endl;
141}
nlohmann::json json
std::set< int > warnedRuns_
runs already reported as using the default
std::vector< double > muMcBot_
double getScale(int run, bool isTop) const
double apply(Track &track, int run) const
double omegaMeanShift(const Track &track, bool isTop) const
RunScaleCorrectionTool(const std::string &runScaleCfg, const std::string &meanCorrCfg="", const std::string &meanCorrVariable="")
std::vector< double > muDataBot_
std::vector< double > edgesBot_
std::vector< double > edgesTop_
std::vector< double > muMcTop_
double lookupValue(const Track &track) const
std::map< int, std::pair< double, double > > scales_
run -> (top, bot)
std::vector< double > muDataTop_
Definition Track.h:32