CMS 3D CMS Logo

SiStripHitEfficiencyHelpers.h
Go to the documentation of this file.
1 #ifndef SiStripHitEfficiencyHelpers_H
2 #define SiStripHitEfficiencyHelpers_H
3 
4 // A bunch of helper functions to deal with menial tasks in the
5 // hit efficiency computation for the PCL workflow
6 
7 // system includes
8 #include <fmt/printf.h>
9 #include <string>
10 
11 // user includes
17 
18 // ROOT includes
19 #include "TEfficiency.h"
20 #include "TProfile.h"
21 #include "TString.h"
22 
23 namespace {
24 
25  enum bounds {
26  k_LayersStart = 0,
27  k_LayersAtTIBEnd = 4,
28  k_LayersAtTOBEnd = 10,
29  k_LayersAtTIDEnd = 13,
30  k_LayersAtTECEnd = 22,
31  k_END_OF_LAYERS = 23,
32  k_END_OF_LAYS_AND_RINGS = 35
33  };
34 
35  /*
36  * for the trend plots of efficiency vs some variable
37  */
38  enum projections { k_vs_LUMI = 0, k_vs_PU = 1, k_vs_BX = 2, k_SIZE = 3 };
39 
40  const std::array<std::string, projections::k_SIZE> projFolder = {{"VsLumi", "VsPu", "VsBx"}};
41  const std::array<std::string, projections::k_SIZE> projFoundHisto = {
42  {"layerfound_vsLumi_layer_", "layerfound_vsPU_layer_", "foundVsBx_layer"}};
43  const std::array<std::string, projections::k_SIZE> projTotalHisto = {
44  {"layertotal_vsLumi_layer_", "layertotal_vsPU_layer_", "totalVsBx_layer"}};
45  const std::array<std::string, projections::k_SIZE> projTitle = {{"Inst Lumi", "Pile-Up", "Bunch Crossing"}};
46  const std::array<std::string, projections::k_SIZE> projXtitle = {
47  {"instantaneous luminosity [Hz/cm^{2}]", "Pile-Up events", "Bunch Crossing number"}};
48 
49  inline void replaceInString(std::string& str, const std::string& from, const std::string& to) {
50  if (from.empty())
51  return;
52  size_t start_pos = 0;
53  while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
54  str.replace(start_pos, from.length(), to);
55  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
56  }
57  }
58 
59  inline unsigned int checkLayer(unsigned int iidd, const TrackerTopology* tTopo) {
60  switch (DetId(iidd).subdetId()) {
62  return tTopo->tibLayer(iidd);
64  return tTopo->tobLayer(iidd) + bounds::k_LayersAtTIBEnd;
66  return tTopo->tidWheel(iidd) + bounds::k_LayersAtTOBEnd;
68  return tTopo->tecWheel(iidd) + bounds::k_LayersAtTIDEnd;
69  default:
70  return bounds::k_LayersStart;
71  }
72  }
73 
74  inline std::string layerName(unsigned int k, const bool showRings, const unsigned int nTEClayers) {
75  const std::string ringlabel{showRings ? "R" : "D"};
76  if (k > bounds::k_LayersStart && k <= bounds::k_LayersAtTIBEnd) {
77  return fmt::format("TIB L{:d}", k);
78  } else if (k > bounds::k_LayersAtTIBEnd && k <= bounds::k_LayersAtTOBEnd) {
79  return fmt::format("TOB L{:d}", k - bounds::k_LayersAtTIBEnd);
80  } else if (k > bounds::k_LayersAtTOBEnd && k <= bounds::k_LayersAtTIDEnd) {
81  return fmt::format("TID {0}{1:d}", ringlabel, k - bounds::k_LayersAtTOBEnd);
82  } else if (k > bounds::k_LayersAtTIDEnd && k <= bounds::k_LayersAtTIDEnd + nTEClayers) {
83  return fmt::format("TEC {0}{1:d}", ringlabel, k - bounds::k_LayersAtTIDEnd);
84  } else {
85  return "should never be here!";
86  }
87  }
88 
89  inline std::string layerSideName(Long_t k, const bool showRings, const unsigned int nTEClayers) {
90  const std::string ringlabel{showRings ? "R" : "D"};
91  if (k > bounds::k_LayersStart && k <= bounds::k_LayersAtTIBEnd) {
92  return fmt::format("TIB L{:d}", k);
93  } else if (k > bounds::k_LayersAtTIBEnd && k <= bounds::k_LayersAtTOBEnd) {
94  return fmt::format("TOB L{:d}", k - bounds::k_LayersAtTIBEnd);
95  } else if (k > bounds::k_LayersAtTOBEnd && k < 14) {
96  return fmt::format("TID- {0}{1:d}", ringlabel, k - bounds::k_LayersAtTOBEnd);
97  } else if (k > 13 && k < 17) {
98  return fmt::format("TID+ {0}{1:d}", ringlabel, k - 13);
99  } else if (k > 16 && k < 17 + nTEClayers) {
100  return fmt::format("TEC- {0}{1:d}", ringlabel, k - 16);
101  } else if (k > 16 + nTEClayers) {
102  return fmt::format("TEC+ {0}{1:d}", ringlabel, k - 16 - nTEClayers);
103  } else {
104  return "shoud never be here!";
105  }
106  }
107 
108  inline double checkConsistency(const StripClusterParameterEstimator::LocalValues& parameters,
109  double xx,
110  double xerr) {
111  double error = sqrt(parameters.second.xx() + xerr * xerr);
112  double separation = abs(parameters.first.x() - xx);
113  double consistency = separation / error;
114  return consistency;
115  }
116 
117  inline bool isDoubleSided(unsigned int iidd, const TrackerTopology* tTopo) {
118  unsigned int layer;
119  switch (DetId(iidd).subdetId()) {
121  layer = tTopo->tibLayer(iidd);
122  return (layer == 1 || layer == 2);
124  layer = tTopo->tobLayer(iidd) + 4;
125  return (layer == 5 || layer == 6);
127  layer = tTopo->tidRing(iidd) + 10;
128  return (layer == 11 || layer == 12);
130  layer = tTopo->tecRing(iidd) + 13;
131  return (layer == 14 || layer == 15 || layer == 18);
132  default:
133  return false;
134  }
135  }
136 
137  inline bool check2DPartner(unsigned int iidd, const std::vector<TrajectoryMeasurement>& traj) {
138  unsigned int partner_iidd = 0;
139  bool found2DPartner = false;
140  // first get the id of the other detector
141  if ((iidd & 0x3) == 1)
142  partner_iidd = iidd + 1;
143  if ((iidd & 0x3) == 2)
144  partner_iidd = iidd - 1;
145  // next look in the trajectory measurements for a measurement from that detector
146  // loop through trajectory measurements to find the partner_iidd
147  for (const auto& tm : traj) {
148  if (tm.recHit()->geographicalId().rawId() == partner_iidd) {
149  found2DPartner = true;
150  }
151  }
152  return found2DPartner;
153  }
154 
155  inline bool isInBondingExclusionZone(
156  unsigned int iidd, unsigned int TKlayers, double yloc, double yErr, const TrackerTopology* tTopo) {
157  constexpr float exclusionWidth = 0.4;
158  constexpr float TOBexclusion = 0.0;
159  constexpr float TECexRing5 = -0.89;
160  constexpr float TECexRing6 = -0.56;
161  constexpr float TECexRing7 = 0.60;
162 
163  //Added by Chris Edelmaier to do TEC bonding exclusion
164  const int subdetector = ((iidd >> 25) & 0x7);
165  const int ringnumber = ((iidd >> 5) & 0x7);
166 
167  bool inZone = false;
168  //New TOB and TEC bonding region exclusion zone
169  if ((TKlayers >= 5 && TKlayers < 11) || ((subdetector == 6) && ((ringnumber >= 5) && (ringnumber <= 7)))) {
170  //There are only 2 cases that we need to exclude for
171  float highzone = 0.0;
172  float lowzone = 0.0;
173  float higherr = yloc + 5.0 * yErr;
174  float lowerr = yloc - 5.0 * yErr;
175  if (TKlayers >= 5 && TKlayers < 11) {
176  //TOB zone
177  highzone = TOBexclusion + exclusionWidth;
178  lowzone = TOBexclusion - exclusionWidth;
179  } else if (ringnumber == 5) {
180  //TEC ring 5
181  highzone = TECexRing5 + exclusionWidth;
182  lowzone = TECexRing5 - exclusionWidth;
183  } else if (ringnumber == 6) {
184  //TEC ring 6
185  highzone = TECexRing6 + exclusionWidth;
186  lowzone = TECexRing6 - exclusionWidth;
187  } else if (ringnumber == 7) {
188  //TEC ring 7
189  highzone = TECexRing7 + exclusionWidth;
190  lowzone = TECexRing7 - exclusionWidth;
191  }
192  //Now that we have our exclusion region, we just have to properly identify it
193  if ((highzone <= higherr) && (highzone >= lowerr))
194  inZone = true;
195  if ((lowzone >= lowerr) && (lowzone <= higherr))
196  inZone = true;
197  if ((higherr <= highzone) && (higherr >= lowzone))
198  inZone = true;
199  if ((lowerr >= lowzone) && (lowerr <= highzone))
200  inZone = true;
201  }
202  return inZone;
203  }
204 
205  struct ClusterInfo {
206  float xResidual;
207  float xResidualPull;
208  float xLocal;
209  ClusterInfo(float xRes, float xResPull, float xLoc) : xResidual(xRes), xResidualPull(xResPull), xLocal(xLoc) {}
210  };
211 
212  inline float calcPhi(float x, float y) {
213  float phi = 0;
214  if ((x >= 0) && (y >= 0))
215  phi = std::atan(y / x);
216  else if ((x >= 0) && (y <= 0))
217  phi = std::atan(y / x) + 2 * M_PI;
218  else if ((x <= 0) && (y >= 0))
219  phi = std::atan(y / x) + M_PI;
220  else
221  phi = std::atan(y / x) + M_PI;
222  phi = phi * 180.0 / M_PI;
223 
224  return phi;
225  }
226 
227  inline TProfile* computeEff(const TH1F* num, const TH1F* denum, const std::string nameHist) {
228  std::string name = "eff_" + nameHist;
229  std::string title = "SiStrip Hit Efficiency" + std::string(num->GetTitle());
230  TProfile* efficHist = new TProfile(name.c_str(),
231  title.c_str(),
232  denum->GetXaxis()->GetNbins(),
233  denum->GetXaxis()->GetXmin(),
234  denum->GetXaxis()->GetXmax());
235 
236  for (int i = 1; i <= denum->GetNbinsX(); i++) {
237  double nNum = num->GetBinContent(i);
238  double nDenum = denum->GetBinContent(i);
239  if (nDenum == 0 || nNum == 0) {
240  continue;
241  }
242  if (nNum > nDenum) {
243  edm::LogWarning("SiStripHitEfficiencyHelpers")
244  << "Alert! specific bin's num is bigger than denum " << i << " " << nNum << " " << nDenum;
245  nNum = nDenum; // set the efficiency to 1
246  }
247  const double effVal = nNum / nDenum;
248  efficHist->SetBinContent(i, effVal);
249  efficHist->SetBinEntries(i, 1);
250  const double errLo = TEfficiency::ClopperPearson((int)nDenum, (int)nNum, 0.683, false);
251  const double errUp = TEfficiency::ClopperPearson((int)nDenum, (int)nNum, 0.683, true);
252  const double errVal = (effVal - errLo > errUp - effVal) ? effVal - errLo : errUp - effVal;
253  efficHist->SetBinError(i, sqrt(effVal * effVal + errVal * errVal));
254 
255  LogDebug("SiStripHitEfficiencyHelpers") << __PRETTY_FUNCTION__ << " " << nameHist << " bin:" << i
256  << " err:" << sqrt(effVal * effVal + errVal * errVal);
257  }
258  return efficHist;
259  }
260 
261  inline Measurement1D computeCPEfficiency(const double num, const double den) {
262  if (den > 0) {
263  const double effVal = num / den;
264  const double errLo = TEfficiency::ClopperPearson((int)den, (int)num, 0.683, false);
265  const double errUp = TEfficiency::ClopperPearson((int)den, (int)num, 0.683, true);
266  const double errVal = (effVal - errLo > errUp - effVal) ? effVal - errLo : errUp - effVal;
267  return Measurement1D(effVal, errVal);
268  } else {
269  return Measurement1D(0., 0.);
270  }
271  }
272 } // namespace
273 #endif
std::pair< LocalPoint, LocalError > LocalValues
unsigned int tobLayer(const DetId &id) const
TString subdetector
unsigned int tidWheel(const DetId &id) const
unsigned int tecWheel(const DetId &id) const
unsigned int tecRing(const DetId &id) const
ring id
T sqrt(T t)
Definition: SSEVec.h:19
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define M_PI
Definition: DetId.h:17
float x
unsigned int tidRing(const DetId &id) const
unsigned int tibLayer(const DetId &id) const
Log< level::Warning, false > LogWarning
#define str(s)
#define LogDebug(id)