CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
L1TkMuMantra.cc
Go to the documentation of this file.
4 
5 #include "TH2.h"
6 #include "TFile.h"
7 
8 using namespace L1TkMuMantraDF;
9 
10 L1TkMuMantra::L1TkMuMantra(const std::vector<double>& bounds,
11  TFile* fIn_theta,
12  TFile* fIn_phi,
13  std::string name = "mantra")
14  : wdws_theta_(bounds.size() - 1, MuMatchWindow()), wdws_phi_(bounds.size() - 1, MuMatchWindow()) {
15  name_ = name;
16 
17  safety_factor_l_ = 0.0;
18  safety_factor_h_ = 0.0;
19 
21 
22  // copy boundaries
23  nbins_ = bounds.size() - 1;
24  bounds_ = bounds;
25 
26  // now load in memory the TF1 fits
27 
28  for (int ib = 0; ib < nbins_; ++ib) {
29  std::string wdn;
30  std::string nml;
31  std::string nmc;
32  std::string nmh;
33  TF1* fl;
34  TF1* fc;
35  TF1* fh;
36 
37  wdn = name_ + std::string("_wdw_theta_") + std::to_string(ib + 1);
38  nml = std::string("fit_low_") + std::to_string(ib + 1);
39  nmc = std::string("fit_cent_") + std::to_string(ib + 1);
40  nmh = std::string("fit_high_") + std::to_string(ib + 1);
41 
42  fl = (TF1*)fIn_theta->Get(nml.c_str());
43  fc = (TF1*)fIn_theta->Get(nmc.c_str());
44  fh = (TF1*)fIn_theta->Get(nmh.c_str());
45  if (fl == nullptr || fc == nullptr || fh == nullptr) {
46  if (verbosity_ > 0) {
47  LogTrace("L1TkMuMantra") << "... fit theta low : " << fl << std::endl;
48  LogTrace("L1TkMuMantra") << "... fit theta cent : " << fc << std::endl;
49  LogTrace("L1TkMuMantra") << "... fit theta high : " << fh << std::endl;
50  }
51  throw cms::Exception("L1TkMuMantra") << "TF1 named " << nml << " or " << nmc << " or " << nmh
52  << " not found in file " << fIn_theta->GetName() << ".\n";
53  }
54  wdws_theta_.at(ib).SetName(wdn);
55  wdws_theta_.at(ib).SetLower(fl);
56  wdws_theta_.at(ib).SetCentral(fc);
57  wdws_theta_.at(ib).SetUpper(fh);
58 
59  wdn = name_ + std::string("_wdw_phi_") + std::to_string(ib + 1);
60  nml = std::string("fit_low_") + std::to_string(ib + 1);
61  nmc = std::string("fit_cent_") + std::to_string(ib + 1);
62  nmh = std::string("fit_high_") + std::to_string(ib + 1);
63  fl = (TF1*)fIn_phi->Get(nml.c_str());
64  fc = (TF1*)fIn_phi->Get(nmc.c_str());
65  fh = (TF1*)fIn_phi->Get(nmh.c_str());
66  if (fl == nullptr || fc == nullptr || fh == nullptr) {
67  if (verbosity_ > 0) {
68  LogTrace("L1TkMuMantra") << "... fit phi low : " << fl << std::endl;
69  LogTrace("L1TkMuMantra") << "... fit phi cent : " << fc << std::endl;
70  LogTrace("L1TkMuMantra") << "... fit phi high : " << fh << std::endl;
71  }
72  throw cms::Exception("L1TkMuMantra") << "TF1 named " << nml << " or " << nmc << " or " << nmh
73  << " not found in file " << fIn_theta->GetName() << ".\n";
74  }
75  wdws_phi_.at(ib).SetName(wdn);
76  wdws_phi_.at(ib).SetLower(fl);
77  wdws_phi_.at(ib).SetCentral(fc);
78  wdws_phi_.at(ib).SetUpper(fh);
79  }
80 }
81 
83  // FIXME: not the most efficient, nor the most elegant implementation for now
84  if (val < bounds_.at(0))
85  return 0;
86  if (val >= bounds_.back())
87  return (nbins_ - 1); // i.e. bounds_size() -2
88 
89  for (uint ib = 0; ib < bounds_.size() - 1; ++ib) {
90  if (val >= bounds_.at(ib) && val < bounds_.at(ib + 1))
91  return ib;
92  }
93 
94  if (verbosity_ > 0)
95  LogTrace("L1TkMuMantra") << "Something strange happened at val " << val << std::endl;
96  return 0;
97 }
98 
99 void L1TkMuMantra::test(double eta, double pt) {
100  int ibin = findBin(eta);
101 
102  LogTrace("L1TkMuMantra") << " ---- eta : " << eta << " pt: " << pt << std::endl;
103  LogTrace("L1TkMuMantra") << " ---- bin " << ibin << std::endl;
104  LogTrace("L1TkMuMantra") << " ---- "
105  << "- low_phi : " << wdws_phi_.at(ibin).bound_low(pt)
106  << "- cent_phi : " << wdws_phi_.at(ibin).bound_cent(pt)
107  << "- high_phi : " << wdws_phi_.at(ibin).bound_high(pt) << std::endl;
108 
109  LogTrace("L1TkMuMantra") << " ---- "
110  << "- low_theta : " << wdws_theta_.at(ibin).bound_low(pt)
111  << "- cent_theta : " << wdws_theta_.at(ibin).bound_cent(pt)
112  << "- high_theta : " << wdws_theta_.at(ibin).bound_high(pt) << std::endl;
113 
114  return;
115 }
116 
117 std::vector<int> L1TkMuMantra::find_match(const std::vector<track_df>& tracks, const std::vector<muon_df>& muons) {
118  std::vector<int> result(muons.size(), -1); // init all TkMu to index -1
119  for (uint imu = 0; imu < muons.size(); ++imu) {
120  muon_df mu = muons.at(imu);
121  std::vector<std::pair<double, int>> matched_trks; // sort_par, idx
122  for (uint itrk = 0; itrk < tracks.size(); ++itrk) {
123  // preselection of tracks
124  track_df trk = tracks.at(itrk);
125  if (trk.chi2 >= max_chi2)
126  continue; // require trk.chi2 < max_chi2
127  if (trk.nstubs < min_nstubs)
128  continue; // require trk.nstubs >= min_nstubs
129 
130  double dphi_charge = reco::deltaPhi(trk.phi, mu.phi) * trk.charge;
131  // sign from theta, to avoid division by 0
132  double dtheta_endc = (mu.theta - trk.theta) * sign(mu.theta);
133  if (sign(mu.theta) != sign(trk.theta)) {
134  // crossing the barrel -> remove 180 deg to the theta of the neg candidate to avoid jumps at eta = 0
135  dtheta_endc -= TMath::Pi();
136  }
137 
138  // lookup the values
139  int ibin = findBin(std::abs(trk.eta));
140 
141  double phi_low = wdws_phi_.at(ibin).bound_low(trk.pt);
142  double phi_cent = wdws_phi_.at(ibin).bound_cent(trk.pt);
143  double phi_high = wdws_phi_.at(ibin).bound_high(trk.pt);
144  relax_windows(phi_low, phi_cent, phi_high); // apply the safety factor
145  bool in_phi = (dphi_charge > phi_low && dphi_charge < phi_high);
146 
147  double theta_low = wdws_theta_.at(ibin).bound_low(trk.pt);
148  double theta_cent = wdws_theta_.at(ibin).bound_cent(trk.pt);
149  double theta_high = wdws_theta_.at(ibin).bound_high(trk.pt);
150  relax_windows(theta_low, theta_cent, theta_high); // apply the safety factor
151  bool in_theta = (dtheta_endc > theta_low && dtheta_endc < theta_high);
152 
153  if (in_phi && in_theta) {
154  double sort_par = 99999;
155  if (sort_type_ == kMaxPt)
156  sort_par = trk.pt;
157  else if (sort_type_ == kMinDeltaPt) {
158  // trk.pt should always be > 0, but put this protection just in case
159  sort_par = (trk.pt > 0 ? std::abs(1. - (mu.pt / trk.pt)) : 0);
160  }
161  matched_trks.emplace_back(sort_par, itrk);
162  }
163  }
164 
165  // choose out of the matched tracks the best one
166  if (!matched_trks.empty()) {
167  sort(matched_trks.begin(), matched_trks.end());
168  int ibest = 99999;
169  if (sort_type_ == kMaxPt)
170  ibest = matched_trks.rbegin()->second; // sorted low to high -> take last for highest pT (rbegin)
171  else if (sort_type_ == kMinDeltaPt)
172  ibest = matched_trks.begin()->second; // sorted low to high -> take first for min pT distance (begin)
173  result.at(imu) = ibest;
174  }
175  }
176 
177  return result;
178 }
179 
180 void L1TkMuMantra::relax_windows(double& low, double cent, double& high) {
181  double delta_high = high - cent;
182  double delta_low = cent - low;
183 
184  high = high + safety_factor_h_ * delta_high;
185  low = low - safety_factor_l_ * delta_low;
186 
187  return;
188 }
189 
191  if (verbosity_ > 0)
192  LogTrace("L1TkMuMantra") << "L1TkMuMantra : setting arbitration type to " << type << std::endl;
193  if (type == "MaxPt")
194  sort_type_ = kMaxPt;
195  else if (type == "MinDeltaPt")
197  else
198  throw cms::Exception("L1TkMuMantra") << "setArbitrationType : cannot understand the arbitration type passed.\n";
199 }
200 
202  // find the boundaries of the match windoww
203  TFile* fIn = TFile::Open(fname.c_str());
204  if (fIn == nullptr) {
205  throw cms::Exception("L1TkMuMantra") << "Can't find file " << fname << " to derive bounds.\n";
206  }
207  TH2* h_test = (TH2*)fIn->Get(hname.c_str());
208  if (h_test == nullptr) {
209  throw cms::Exception("L1TkMuMantra") << "Can't find histo " << hname << " in file " << fname
210  << " to derive bounds.\n";
211  }
212 
213  int nbds = h_test->GetNbinsY() + 1;
214  std::vector<double> bounds(nbds);
215  for (int ib = 0; ib < nbds; ++ib) {
216  bounds.at(ib) = h_test->GetYaxis()->GetBinLowEdge(ib + 1);
217  }
218  fIn->Close();
219  return bounds;
220 }
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
const double Pi
int ib
Definition: cuy.py:661
int findBin(double val)
Definition: L1TkMuMantra.cc:82
sortParType sort_type_
Definition: L1TkMuMantra.h:120
L1TkMuMantra(const std::vector< double > &bounds, TFile *fIn_theta, TFile *fIn_phi, std::string name)
Definition: L1TkMuMantra.cc:10
auto const & tracks
cannot be loose
double max_chi2
Definition: L1TkMuMantra.h:110
std::string name_
Definition: L1TkMuMantra.h:102
#define LogTrace(id)
tuple result
Definition: mps_fire.py:311
void relax_windows(double &low, double cent, double &high)
static std::vector< double > prepare_corr_bounds(std::string fname, std::string hname)
std::vector< MuMatchWindow > wdws_phi_
Definition: L1TkMuMantra.h:107
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const int mu
Definition: Constants.h:22
int sign(double x)
Definition: L1TkMuMantra.h:73
float safety_factor_h_
Definition: L1TkMuMantra.h:113
float safety_factor_l_
Definition: L1TkMuMantra.h:112
string fname
main script
void test(double eta, double pt)
Definition: L1TkMuMantra.cc:99
std::vector< MuMatchWindow > wdws_theta_
Definition: L1TkMuMantra.h:106
tuple muons
Definition: patZpeak.py:39
std::vector< int > find_match(const std::vector< L1TkMuMantraDF::track_df > &tracks, const std::vector< L1TkMuMantraDF::muon_df > &muons)
void setArbitrationType(std::string type)
std::vector< double > bounds_
Definition: L1TkMuMantra.h:105
tuple size
Write out results.