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
TrackSmearingTool.h
Go to the documentation of this file.
1#pragma once
2
3//------------------//
4// C++ //
5//------------------//
6#include <iostream>
7#include <random>
8#include <memory>
9#include <fstream>
10#include <algorithm>
11#include <cmath>
12#include <map>
13#include <string>
14#include <vector>
15
16//------------------//
17// JSON //
18//------------------//
19#include "json.hpp"
20using json = nlohmann::json;
21
22//------------------//
23// hpstr //
24//------------------//
25
26#include "Track.h"
27#include "Vertex.h"
28#include "Particle.h"
29#include "MCParticle.h"
30#include "TrackerHit.h"
31
32class TFile;
33class TH1D;
34
36
37 public :
38
39 // Constructor that auto-detects file type based on extension:
40 // - .root files: histogram-based smearing
41 // - .json files: JSON config with top/bot values
42 // The seed needs to be set accordingly for each instance / job of the smearing tool
43 // The smearingFactor multiplies all smearing parameters (default 1.0)
44 // relSmearingP/relSmearingZ0 control whether smearing is relative (multiplicative) or absolute (additive)
45 TrackSmearingTool(const std::string& smearingfile,
46 const bool relSmearingP = true,
47 const bool relSmearingZ0 = false,
48 const int seed = 42,
49 const std::string& tracks = "KalmanFullTracks",
50 const double smearingFactor = 1.0);
51
52 // Constructor with fixed smearing values (no histogram lookup)
53 // The smearingFactor multiplies all smearing parameters (default 1.0)
54 // relSmearingP/relSmearingZ0 control whether smearing is relative (multiplicative) or absolute (additive)
55 TrackSmearingTool(const double pSmearingValue,
56 const double z0SmearingValue,
57 const bool relSmearingP = true,
58 const bool relSmearingZ0 = false,
59 const int seed = 42,
60 const double smearingFactor = 1.0);
61
62 double smearTrackP(const Track& trk);
63 double smearTrackP(const double p);
64 double updateWithSmearP(Track& trk);
65 void updateVertexWithSmearP(Vertex* vtx, double p1_smear_factor, double p2_smear_factor);
66
67 // Omega (curvature) smearing - alternative to momentum smearing
68 // Smears omega directly, then recalculates momentum from smeared omega
69 // Returns the scale factor applied to momentum (smeared_p / original_p)
70 double updateWithSmearOmega(Track& trk, double bfield = 0.52);
71
72 double smearTrackZ0(const Track& trk);
73 double smearTrackZ0(const double z0);
74 void updateWithSmearZ0(Track& trk);
75
76 // Truth matching support
77 // Set the MC particles collection for truth matching (call once per event)
78 void setMCParticles(const std::vector<MCParticle*>* mc_particles);
79
80 // Enable/disable truth matching requirement (only smear tracks with truth match)
81 void setRequireTruthMatch(bool require) { requireTruthMatch_ = require; }
82 void setDebug(bool debug) { debug_ = debug; }
83
84 // Declare whether this instance is processing data (true) or MC (false).
85 // - MC mode (default): applies Gaussian smearing only.
86 // - Data mode: applies mean correction only (shifts data mean toward MC mean);
87 // no Gaussian smearing is applied. Requires mean values in the JSON and
88 // setApplyMeanCorr(true).
89 void setIsData(bool isData) { isData_ = isData; }
90
91 // Enable the mean correction. Has no effect in MC mode.
92 // For relative smearing (p): scale factor mu_mc/mu_data applied to data.
93 // For absolute smearing (z0, omega): additive shift mu_mc - mu_data.
94 void setApplyMeanCorr(bool apply) { applyMeanCorr_ = apply; }
95
96 // Print a full human-readable summary of the loaded configuration.
97 // Call this after all setters (setIsData, setForcedVariable, …) are done.
98 void printConfig() const;
99
100 // Explicitly select which smearing parameterization to use.
101 // "flat" -> use scalar top/bot values, disable binned lookup
102 // "nHits", "tanLambda", "phi0" -> require binned lookup with that variable;
103 // throws if the JSON does not contain a matching binned section.
104 // "" -> use whatever the JSON provides (default, not recommended)
105 void setForcedVariable(const std::string& var);
106
107 // Override the parameterization used for scale (mean) corrections in data mode.
108 // When set to a variable name (e.g. "tanLambda"), the data-mode omega smearing
109 // derives the scale factor from pSmearing_binned_{var} mu_data/mu_mc values,
110 // applying a p-space multiplicative correction rather than an additive omega shift.
111 // By default (empty string) the scale correction comes from omegaSmearing_binned_{binnedLookupVariable_}.
112 void setScaleCorrVariable(const std::string& var);
113
114 // Check if a track has a truth-matched electron/positron (abs(PDG) == 11)
115 // Returns true if matched, false if no match or MC info unavailable
116 bool hasTruthMatch(Track& trk);
117
118 private:
119 // Get the PDG code of the truth particle best matching this track
120 int getTruthPDG(Track& trk);
121
122 // Binned smearing lookup helpers
123 // Returns the smearing value for the given x using linear bin lookup
124 double lookupBinnedValue(const std::vector<double>& edges,
125 const std::vector<double>& values,
126 double x) const;
127 // Returns the lookup variable value from the track (tanLambda or phi0)
128 double getLookupValue(const Track& track) const;
129
130 //Random engine
131 std::shared_ptr<std::default_random_engine> generator_;
132
133 // General Normal distributions
134
135 std::shared_ptr<std::normal_distribution<double>> normal_;
136
137 std::shared_ptr<TFile> smearingfile_;
138
139 //Smearing terms
140 TH1D* smearing_histo_top_{nullptr};
141 TH1D* smearing_histo_bot_{nullptr};
142
143 // Fixed smearing values (used when constructed with fixed values)
144 double pSmearingValue_{0.};
146 bool useFixedSmearing_{false};
147
148 // Separate top/bottom smearing values (used when loaded from JSON config)
154
155 // Omega (curvature) smearing values
158 bool smearOmega_{false}; // If true, use omega smearing instead of p smearing
159
160 // Binned smearing lookup tables (from hpsplot tool JSON).
161 // Each map key is a variable name (e.g. "tanLambda", "phi0", "nHits").
162 // Keys come from JSON entries named {section}_binned_{varname}.
163 struct BinnedParam {
164 std::vector<double> edgesTop, edgesBot;
165 std::vector<double> valTop, valBot; // sigma values; empty for z0 (mean-only)
166 std::vector<double> muDatTop, muDatBot; // mean corrections
167 std::vector<double> muMcTop, muMcBot;
168 };
169 std::string binnedLookupVariable_{"tanLambda"}; // active variable; set by setForcedVariable
170 std::string scaleCorrVariable_{""}; // if set, omega data-mode scale uses pBinned_ means
171 std::map<std::string, BinnedParam> pBinned_;
172 std::map<std::string, BinnedParam> omegaBinned_;
173 std::map<std::string, BinnedParam> z0Binned_;
174
175 // Mean corrections (from mu_data / mu_mc in the tool JSON)
176 // Scalar — from 1D fits for top/bot regions
177 double pMeanDataTop_{0.}, pMeanMcTop_{0.};
178 double pMeanDataBot_{0.}, pMeanMcBot_{0.};
183 bool hasMeanCorrP_{false};
184 bool hasMeanCorrZ0_{false};
185 bool hasMeanCorrOmega_{false};
186 bool applyMeanCorr_{false};
187 bool isData_{false};
188
189 std::string smearingFile_{""}; // path stored for printConfig()
190
191 // debug
192 bool debug_{false};
193 bool relSmearingP_{false};
194 bool relSmearingZ0_{false};
195
196 // factor to multiply all smearing parameters by
197 double smearingFactor_{1.0};
198
199 // Truth matching
200 const std::vector<MCParticle*>* mcParticles_{nullptr};
202
203};
nlohmann::json json
Class used to encapsulate information about a mc particle.
Class used to encapsulate information about a particle.
Class used to encapsulate track information.
Class used to encapsulate tracker hit information.
Class used to encapsulate Vertex information.
double smearTrackP(const Track &trk)
double smearTrackZ0(const Track &trk)
bool hasTruthMatch(Track &trk)
double lookupBinnedValue(const std::vector< double > &edges, const std::vector< double > &values, double x) const
TrackSmearingTool(const std::string &smearingfile, const bool relSmearingP=true, const bool relSmearingZ0=false, const int seed=42, const std::string &tracks="KalmanFullTracks", const double smearingFactor=1.0)
std::map< std::string, BinnedParam > z0Binned_
void updateWithSmearZ0(Track &trk)
void updateVertexWithSmearP(Vertex *vtx, double p1_smear_factor, double p2_smear_factor)
std::string scaleCorrVariable_
int getTruthPDG(Track &trk)
void setForcedVariable(const std::string &var)
std::shared_ptr< std::default_random_engine > generator_
std::shared_ptr< TFile > smearingfile_
void setMCParticles(const std::vector< MCParticle * > *mc_particles)
void setDebug(bool debug)
void setRequireTruthMatch(bool require)
std::shared_ptr< std::normal_distribution< double > > normal_
std::string binnedLookupVariable_
const std::vector< MCParticle * > * mcParticles_
std::map< std::string, BinnedParam > pBinned_
double updateWithSmearOmega(Track &trk, double bfield=0.52)
void setIsData(bool isData)
double updateWithSmearP(Track &trk)
double getLookupValue(const Track &track) const
void setApplyMeanCorr(bool apply)
void setScaleCorrVariable(const std::string &var)
std::map< std::string, BinnedParam > omegaBinned_
Definition Track.h:32