CMS 3D CMS Logo

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