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
TrackBiasingTool.cxx
Go to the documentation of this file.
1#include "TrackBiasingTool.h"
2#include "TFile.h"
3#include "TH1D.h"
4#include "TVector3.h"
5
6#include <stdexcept>
7#include <fstream>
8
9TrackBiasingTool::TrackBiasingTool(const std::string& biasingfile,
10 const std::string& tracks) {
11
12 if (biasingfile.empty()) {
13 // z0-calibration-only mode: no momentum biasing ROOT file
14 eop_h_top_ = nullptr;
15 eop_h_bot_ = nullptr;
16 return;
17 }
18
19 biasingfile_ = std::make_shared<TFile>(biasingfile.c_str());
20
21 if (!biasingfile_)
22 throw std::invalid_argument("Provided input biasing file doesn't exists");
23
24 eop_h_top_ = (TH1D*) biasingfile_->Get((tracks+"_eop_vs_charge_top").c_str());
25 eop_h_bot_ = (TH1D*) biasingfile_->Get((tracks+"_eop_vs_charge_bot").c_str());
26
27 if (!eop_h_top_ || !eop_h_bot_)
28 throw std::invalid_argument("Top and Bottom biasing histograms not found in smearing file");
29
30}
31
32
33double TrackBiasingTool::getCorrection(const double& p,
34 const double tanL,
35 const int q) {
36
37 TH1D* bias_histo_ = (tanL > 0.) ? eop_h_top_ : eop_h_bot_;
38 int binN = bias_histo_->GetXaxis()->FindBin(q);
39
40 return bias_histo_->GetBinContent(binN);
41
42}
43
44
46
47 double p = trk.getP();
48 double q = trk.getCharge();
49
50 TH1D* bias_histo_ = (trk.getTanLambda() > 0.) ? eop_h_top_ : eop_h_bot_;
51
52 int binN = bias_histo_->GetXaxis()->FindBin(q);
53 if (debug_)
54 std::cout<<"Track charge="<<q<<" bin="<<binN<<std::endl;
55
56 double eop_bias = bias_histo_->GetBinContent(binN);
57
58 double pcorr = p * eop_bias;
59
60 if (debug_)
61 std::cout<<"Original p = " << p <<" corrected p="<< pcorr <<std::endl;
62
63 return pcorr;
64}
65
67
68 double biased_p = biasTrackP(trk);
69
70 std::vector<double> momentum = trk.getMomentum();
71 double unbiasedp = trk.getP();
72
73 for (double& coordinate : momentum)
74 coordinate *= (biased_p / unbiasedp);
75
76 trk.setMomentum(momentum);
77
78}
79
80// This will recompute the track momentum from curvature and store it in the track
81void TrackBiasingTool::updateWithBiasP(Track& trk, double scaleFactor) {
82
83 std::vector<double> momentum = trk.getMomentum();
84
85 for (double& coordinate : momentum)
86 coordinate *= scaleFactor ;
87
88 trk.setMomentum(momentum);
89
90}
91
92void TrackBiasingTool::loadZ0Calibration(const std::string& jsonFile) {
93 std::ifstream ifs(jsonFile);
94 if (!ifs.is_open())
95 throw std::invalid_argument("TrackBiasingTool: cannot open z0 calibration file: " + jsonFile);
96 json cfg;
97 ifs >> cfg;
98
99 auto loadParams = [](const json& node, const std::string& key) -> Z0CalibParams {
101 if (node.contains(key)) {
102 p.slope = node[key].value("slope", 0.0);
103 p.intercept = node[key].value("intercept", 0.0);
104 }
105 return p;
106 };
107
108 for (auto& [runStr, runCfg] : cfg.items()) {
109 // "default" key is treated as run 0 (always selected by largest-key-<=run logic)
110 int runKey = (runStr == "default") ? 0 : std::stoi(runStr);
111 Z0CalibSet s;
112 s.ele_top = loadParams(runCfg, "ele_top");
113 s.ele_bot = loadParams(runCfg, "ele_bot");
114 s.pos_top = loadParams(runCfg, "pos_top");
115 s.pos_bot = loadParams(runCfg, "pos_bot");
116 z0CalibByRun_[runKey] = s;
117 }
118
119 z0CalibLoaded_ = true;
120 std::cout << "TrackBiasingTool: loaded z0 calibration from " << jsonFile
121 << " (" << z0CalibByRun_.size() << " run entries)" << std::endl;
122}
123
125 if (!z0CalibLoaded_ || run == z0CalibCurrentRun_) return;
126
127 // Find largest key <= run
128 int best = -1;
129 for (auto& [key, set] : z0CalibByRun_) {
130 if (key <= run) best = key;
131 }
132
133 if (best < 0) {
134 std::cout << "TrackBiasingTool: WARNING no z0 calibration entry found for run "
135 << run << " (all keys > run), z0 calibration will be skipped." << std::endl;
136 z0CalibLoaded_ = false;
137 return;
138 }
139
140 z0CalibActive_ = z0CalibByRun_.at(best);
141 z0CalibCurrentRun_ = run;
142
143 if (debug_) {
144 std::cout << "TrackBiasingTool::setRun(" << run << "): using calibration key=" << best << std::endl;
145 std::cout << " ele_top: slope=" << z0CalibActive_.ele_top.slope << " intercept=" << z0CalibActive_.ele_top.intercept << std::endl;
146 std::cout << " ele_bot: slope=" << z0CalibActive_.ele_bot.slope << " intercept=" << z0CalibActive_.ele_bot.intercept << std::endl;
147 std::cout << " pos_top: slope=" << z0CalibActive_.pos_top.slope << " intercept=" << z0CalibActive_.pos_top.intercept << std::endl;
148 std::cout << " pos_bot: slope=" << z0CalibActive_.pos_bot.slope << " intercept=" << z0CalibActive_.pos_bot.intercept << std::endl;
149 }
150}
151
153 if (!z0CalibLoaded_) return;
154
155 bool isTop = trk.getTanLambda() > 0.0;
156 bool isEle = trk.getCharge() < 0;
157
158 const Z0CalibParams& p =
159 isEle ? (isTop ? z0CalibActive_.ele_top : z0CalibActive_.ele_bot)
160 : (isTop ? z0CalibActive_.pos_top : z0CalibActive_.pos_bot);
161
162 double tanL = trk.getTanLambda();
163 double z0 = trk.getZ0();
164 double delta = p.slope * tanL + p.intercept;
165 double z0new = z0 - delta;
166
167 if (debug_) {
168 std::cout << "TrackBiasingTool::updateWithCalibrateZ0:"
169 << " type=" << (isEle ? "ele" : "pos") << "_" << (isTop ? "top" : "bot")
170 << " |tanL|=" << tanL
171 << " slope=" << p.slope << " intercept=" << p.intercept
172 << " delta=" << delta
173 << " z0=" << z0 << " -> " << z0new << std::endl;
174 }
175
176 trk.setZ0(z0new);
177}
178
180
181 //Correct the vertex
182 double corr1 = getCorrection(vtx->getP1().Mag(), vtx->getP1Y(), -1); //ele
183 double corr2 = getCorrection(vtx->getP2().Mag(), vtx->getP2Y(), 1); //pos
184
185
186 TVector3 p1_corr, p2_corr;
187 double m_corr;
188
189 p1_corr.SetX(vtx->getP1X()*corr1);
190 p1_corr.SetY(vtx->getP1Y()*corr1);
191 p1_corr.SetZ(vtx->getP1Z()*corr1);
192
193 p2_corr.SetX(vtx->getP2X()*corr2);
194 p2_corr.SetY(vtx->getP2Y()*corr2);
195 p2_corr.SetZ(vtx->getP2Z()*corr2);
196
197 m_corr = vtx->getInvMass() * sqrt(corr1*corr2);
198
199 vtx->setVtxParameters(p1_corr, p2_corr, m_corr);
200
201}
202
203
nlohmann::json json
Z0CalibSet z0CalibActive_
std::map< int, Z0CalibSet > z0CalibByRun_
double getCorrection(const double &p, const double tanL, const int q)
TrackBiasingTool(const std::string &biasingfile, const std::string &tracks="KalmanFullTracks")
void loadZ0Calibration(const std::string &jsonFile)
void updateWithBiasP(Track &trk, double scaleFactor)
void updateVertexWithBiasP(Vertex *vtx)
double biasTrackP(const Track &track)
void updateWithCalibrateZ0(Track &trk)
std::shared_ptr< TFile > biasingfile_
Definition Track.h:32
int getCharge() const
Definition Track.h:249
double getTanLambda() const
Definition Track.h:92
std::vector< double > getMomentum()
Definition Track.h:287
double getZ0() const
Definition Track.h:93
void setZ0(const double z0)
Definition Track.h:94
double getP() const
Definition Track.h:293
void setMomentum(double bfield=0.52)
Definition Track.cxx:63