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.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TrackSmearingTool.cxx
Go to the documentation of this file.
1#include "TrackSmearingTool.h"
2#include "TFile.h"
3#include "TH1D.h"
4
5#include <stdexcept>
6
7TrackSmearingTool::TrackSmearingTool(const std::string& smearingfile,
8 const bool relSmearing,
9 const int seed,
10 const std::string& tracks){
11
12
13 relSmearing_ = relSmearing;
14 std::string hsuffix = relSmearing_ ? "_rel" : "";
15 smearingfile_ = std::make_shared<TFile>(smearingfile.c_str());
16
17 if (!smearingfile_)
18 throw std::invalid_argument( "Provided input smearing file does not exists");
19
20 //cache the smearing histograms
21 smearing_histo_top_ = (TH1D*) smearingfile_->Get((tracks+"_p_vs_nHits_hh_smearing"+hsuffix).c_str());
22 smearing_histo_bot_ = (TH1D*) smearingfile_->Get((tracks+"_p_vs_nHits_hh_smearing"+hsuffix).c_str());
23
25 throw std::invalid_argument("Top and Bottom smearing histograms not found in smearing file");
26
27 //setup random engine
28 if (debug_)
29 std::cout<<"Setting up random engine with seed "<<seed<<std::endl;
30 generator_ = std::make_shared<std::default_random_engine>(seed);
31
32 normal_ = std::make_shared<std::normal_distribution<double>>(0.,1.);
33
34}
35
37
38 double p = track.getP();
39 double nhits = track.getTrackerHitCount();
40 bool isTop = track.getTanLambda() > 0. ? true : false;
41 int binN = smearing_histo_top_->FindBin(nhits);
42
43 if (debug_)
44 std::cout<<"Track nhits="<<nhits<<" bin="<<binN<<std::endl;
45
46 if (binN < 1) {
47 if (debug_)
48 std::cout<<"Track nhits="<<nhits<<" bin="<<binN<<" rounding to bin=1"<< std::endl;
49 binN=1;
50 } else if (binN > smearing_histo_top_->GetXaxis()->GetNbins()) {
51 throw std::invalid_argument("Bin not found in smearing histogram");
52 }
53
54 double rel_smear = (*normal_)(*generator_);
55 double sp = 0.;
56
57 if (isTop)
58 sp = rel_smear * smearing_histo_top_->GetBinContent(binN);
59 else
60 sp = rel_smear * smearing_histo_bot_->GetBinContent(binN);
61
62 double psmear = 0.;
63
64 if (relSmearing_)
65 psmear = p + sp*p;
66 else
67 psmear = p + sp;
68
69
70 if (debug_) {
71 std::cout<<"Track isTop: "<<isTop<<" nHits: "<<nhits<<" p: "<<p<<" deltaP=" << sp<<" p'="<<psmear<<std::endl;
72 }
73
74 return psmear;
75
76}
77
79 double smeared_magnitude = smearTrackP(trk);
80 // updated momentum by scaling each coordinate by smeared/unsmeared
81 // this takes the direction of the unsmeared momentum and applies
82 // the smeared magnitude
83 std::vector<double> momentum = trk.getMomentum();
84 double unsmeared_magnitude = trk.getP();
85 for (double& coordinate : momentum)
86 coordinate *= (smeared_magnitude/unsmeared_magnitude);
87 trk.setMomentum(momentum);
88}
89
double smearTrackP(const Track &trk)
TrackSmearingTool(const std::string &smearingfile, const bool relSmearing=true, const int seed=42, const std::string &tracks="KalmanFullTracks")
std::shared_ptr< std::default_random_engine > generator_
std::shared_ptr< TFile > smearingfile_
std::shared_ptr< std::normal_distribution< double > > normal_
void updateWithSmearP(Track &trk)
Definition Track.h:32
std::vector< double > getMomentum()
Definition Track.h:285
double getP() const
Definition Track.h:291
void setMomentum(double bfield=0.52)
Definition Track.cxx:63