CMS 3D CMS Logo

FWTrackResidualDetailView.cc
Go to the documentation of this file.
1 
2 #include "TVector3.h"
3 #include "TH2.h"
4 #include "TLine.h"
5 #include "TLatex.h"
6 #include "TPaveText.h"
7 #include "TCanvas.h"
8 #include "TEveWindow.h"
9 
13 
20 
21 using reco::HitPattern;
22 using reco::Track;
23 using reco::TrackBase;
25 
26 const char* FWTrackResidualDetailView::m_det_tracker_str[6] = {"PXB", "PXF", "TIB", "TID", "TOB", "TEC"};
27 
29  : m_ndet(0),
30  m_nhits(0),
31  m_resXFill(3007),
32  m_resXCol(kGreen - 9),
33  m_resYFill(3006),
34  m_resYCol(kWhite),
35  m_stereoFill(3004),
36  m_stereoCol(kCyan - 9),
37  m_invalidFill(3001),
38  m_invalidCol(kRed) {}
39 
41 
43  auto const& residuals = track->residuals();
44 
45  const FWGeometry* geom = id.item()->getGeom();
46  assert(geom != nullptr);
47 
48  const HitPattern& hitpat = track->hitPattern();
50  hittype.reserve(m_nhits);
51  stereo.reserve(m_nhits);
52  subsubstruct.reserve(m_nhits);
53  substruct.reserve(m_nhits);
54  m_detector.reserve(m_nhits);
55  res[0].reserve(m_nhits);
56  res[1].reserve(m_nhits);
57 
58  for (int i = 0; i < m_nhits; ++i) {
59  //printf("there are %d hits in the pattern, %d in the vector, this is %u\n",
60  // m_nhits, track->recHitsEnd() - track->recHitsBegin(), (*(track->recHitsBegin() + i))->geographicalId().rawId());
62  hittype.push_back(HitPattern::getHitType(pattern));
63  stereo.push_back(HitPattern::getSide(pattern));
64  subsubstruct.push_back(HitPattern::getSubSubStructure(pattern));
65  substruct.push_back(HitPattern::getSubStructure(pattern));
66  m_detector.push_back(HitPattern::getSubDetector(pattern));
67  if ((*(track->recHitsBegin() + i))->isValid()) {
68  res[0].push_back(
69  getSignedResidual(geom, (*(track->recHitsBegin() + i))->geographicalId().rawId(), residuals.pullX(i)));
70  } else {
71  res[0].push_back(0);
72  }
73  res[1].push_back(residuals.pullY(i));
74  // printf("%s, %i\n",m_det_tracker_str[substruct[i]-1],subsubstruct[i]);
75  }
76 
77  m_det[0] = 0;
78  for (int j = 0; j < m_nhits - 1;) {
79  int k = j + 1;
80  for (; k < m_nhits; k++) {
81  if (substruct[j] == substruct[k] && subsubstruct[j] == subsubstruct[k]) {
82  if (k == (m_nhits - 1))
83  j = k;
84  } else {
85  m_ndet++;
86  j = k;
87  m_det[m_ndet] = j;
88  break;
89  }
90  }
91  }
92  m_ndet++;
93  m_det[m_ndet] = m_nhits;
94  // printDebug();
95 }
96 
98  if (!track->extra().isAvailable()) {
99  fwLog(fwlog::kError) << " no track extra information is available.\n";
100  m_viewCanvas->cd();
101  TLatex* latex = new TLatex();
102  latex->SetTextSize(0.1);
103  latex->DrawLatex(0.1, 0.5, "No track extra information");
104  return;
105  }
106  prepareData(id, track);
107 
108  // draw histogram
109  m_viewCanvas->cd();
110  m_viewCanvas->SetHighLightColor(-1);
111  TH2F* h_res = new TH2F("h_resx", "h_resx", 10, -5.5, 5.5, m_ndet, 0, m_ndet);
112  TPad* padX = new TPad("pad1", "pad1", 0.2, 0., 0.8, 0.99);
113  padX->SetBorderMode(0);
114  padX->SetLeftMargin(0.2);
115  padX->Draw();
116  padX->cd();
117  padX->SetFrameLineWidth(0);
118  padX->Modified();
119  h_res->SetDirectory(nullptr);
120  h_res->SetStats(kFALSE);
121  h_res->SetTitle("");
122  h_res->SetXTitle("residual");
123  h_res->GetXaxis()->SetTickLength(0);
124  h_res->GetYaxis()->SetTickLength(0);
125  h_res->GetXaxis()->SetNdivisions(20);
126  h_res->GetYaxis()->SetLabelSize(0.06);
127  h_res->Draw();
128  padX->SetGridy();
129 
130  float larray[9] = {0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.5, 4.5, 5.5};
131  float larray2[8];
132  for (int l = 0; l < 8; l++) {
133  float diff2 = (larray[l + 1] - larray[l]) / 2;
134  larray2[l] = larray[l] + diff2;
135  // printf("(%.1f,%.1f),",larray[i],larray[i+1]);
136  }
137 
138  int resi[2][64];
139  for (int l = 0; l < m_nhits; l++) {
140  for (int k = 0; k < 8; k++) {
141  if (fabs(res[0][l]) == larray2[k])
142  resi[0][l] = k;
143  if (fabs(res[1][l]) == larray2[k])
144  resi[1][l] = k;
145  }
146  }
147 
148  TLine* lines[17];
149  for (int l = 0; l < 17; l++) {
150  int ix = l % 9;
151  int sign = 1;
152  sign = (l > 8) ? -1 : 1;
153  lines[l] = new TLine(sign * larray[ix], 0, sign * larray[ix], m_ndet);
154  if (l != 9)
155  lines[l]->SetLineStyle(3);
156  padX->cd();
157  lines[l]->Draw();
158  }
159 
160  float width = 0.25;
161  int filltype;
162  Color_t color;
163  Double_t box[4];
164  padX->cd();
165 
166  for (int h = 0; h < 2; h++) {
167  float height1 = 0;
168  for (int j = 0; j < m_ndet; j++) {
169  // take only X res and Y pixel residals
170  if (strcmp(m_det_tracker_str[substruct[m_det[j]] - 1], "PXB") && h)
171  continue;
172 
173  char det_str2[256];
174  snprintf(det_str2, 255, "%s/%i", m_det_tracker_str[substruct[m_det[j]] - 1], subsubstruct[m_det[j]]);
175  h_res->GetYaxis()->SetBinLabel(j + 1, det_str2);
176 
177  int diff = m_det[j + 1] - m_det[j];
178  int k = 0;
179  width = 1.0 / diff;
180 
181  for (int l = m_det[j]; l < (m_det[j] + diff); l++) {
182  // g->SetPoint(l,resx[l],j+0.5);
183  // printf("%i, %f %f %f\n",l,resx[l],sign*larray[resxi[l]],sign*larray[resxi[l]+1]);
184  int sign = (res[h][l] < 0) ? -1 : 1;
185  box[0] = (hittype[l] == 0) ? sign * larray[resi[h][l]] : -5.5;
186  box[2] = (hittype[l] == 0) ? sign * larray[resi[h][l] + 1] : 5.5;
187  box[1] = height1 + width * k;
188  box[3] = height1 + width * (k + 1);
189 
190  if (stereo[l] == 1) {
191  color = m_stereoCol;
192  filltype = m_stereoFill;
193  } else if (hittype[l] != 0) {
194  color = m_invalidCol;
195  filltype = m_invalidFill;
196  } else {
197  filltype = h ? m_resYFill : m_resXFill;
198  color = h ? m_resYCol : m_resXCol;
199  }
200 
201  drawCanvasBox(box, color, filltype, h < 1);
202  k++;
203  }
204  height1 += 1;
205  }
206  }
207 
208  // title
209  const char* res_str = "residuals in Si detector local x-y coord.";
210  TPaveText* pt = new TPaveText(0.0, 0.91, 1, 0.99, "blNDC");
211  pt->SetBorderSize(0);
212  pt->SetFillColor(kWhite);
213  pt->AddText(res_str);
214  pt->Draw();
215 
216  m_viewCanvas->cd();
217  m_viewCanvas->SetEditable(kFALSE);
218 
219  setTextInfo(id, track);
220 }
221 
222 double FWTrackResidualDetailView::getSignedResidual(const FWGeometry* geom, unsigned int id, double resX) {
223  double local1[3] = {0, 0, 0};
224  double local2[3] = {resX, 0, 0};
225  double global1[3], global2[3];
226  const TGeoMatrix* m = geom->getMatrix(id);
227  assert(m != nullptr);
228  m->LocalToMaster(local1, global1);
229  m->LocalToMaster(local2, global2);
230  TVector3 g1 = global1;
231  TVector3 g2 = global2;
232  if (g2.DeltaPhi(g1) > 0)
233  return resX;
234  else
235  return -resX;
236 }
237 
239  for (int i = 0; i < m_ndet; i++) {
240  std::cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << std::endl;
241  std::cout << "idx " << i << " det[idx] " << m_det[i] << std::endl;
242  std::cout << "m_det idx " << m_det[i] << std::endl;
243  std::cout << "m_det_tracker_str idx " << substruct[m_det[i]] - 1 << std::endl;
244  printf("m_det[idx] %i m_det_tracker_str %s substruct %i\n",
245  m_det[i],
247  subsubstruct[m_det[i]]);
248  }
249 }
250 
251 void FWTrackResidualDetailView::setTextInfo(const FWModelId& /*id*/, const reco::Track* /*track*/) {
252  m_infoCanvas->cd();
253 
254  char mytext[256];
255  Double_t fontsize = 0.07;
256  TLatex* latex = new TLatex();
257  latex->SetTextSize(fontsize);
258  latex->Draw();
259  Double_t x0 = 0.02;
260  Double_t y = 0.95;
261 
262  // summary
263  int nvalid = 0;
264  int npix = 0;
265  int nstrip = 0;
266  for (int i = 0; i < m_nhits; i++) {
267  if (hittype[i] == 0)
268  nvalid++;
269  if (substruct[i] < 3)
270  npix++;
271  else
272  nstrip++;
273  }
274 
275  latex->SetTextSize(fontsize);
276  Double_t boxH = 0.25 * fontsize;
277 
278  double yStep = 0.04;
279 
280  latex->DrawLatex(x0, y, "Residual:");
281  y -= yStep;
282  latex->DrawLatex(
283  x0,
284  y,
285  "sgn(#hat{X}#bullet#hat{#phi}) #times #frac{X_{hit} - X_{traj}}{#sqrt{#sigma^{2}_{hit} + #sigma^{2}_{traj}}}");
286  y -= 2.5 * yStep;
287  snprintf(mytext, 255, "layers hit: %i", m_ndet);
288  latex->DrawLatex(x0, y, mytext);
289  y -= yStep;
290  snprintf(mytext, 255, "valid Si hits: %i", nvalid);
291  latex->DrawLatex(x0, y, mytext);
292  y -= yStep;
293  snprintf(mytext, 255, "total Si hits: %i", m_nhits);
294  latex->DrawLatex(x0, y, mytext);
295  y -= yStep;
296  snprintf(mytext, 255, "valid Si pixel hits: %i", npix);
297  latex->DrawLatex(x0, y, mytext);
298  y -= yStep;
299  snprintf(mytext, 255, "valid Si strip hits: %i", nstrip);
300  latex->DrawLatex(x0, y, mytext);
301 
302  Double_t pos[4];
303  pos[0] = 0.4;
304  pos[2] = 0.55;
305 
306  y -= yStep * 2;
307  latex->DrawLatex(x0, y, "X hit");
308  pos[1] = y;
309  pos[3] = pos[1] + boxH;
311 
312  y -= yStep;
313  latex->DrawLatex(x0, y, "Y hit");
314  pos[1] = y;
315  pos[3] = pos[1] + boxH;
317 
318  y -= yStep;
319  latex->DrawLatex(x0, y, "stereo hit");
320  pos[1] = y;
321  pos[3] = pos[1] + boxH;
323 
324  y -= yStep;
325  latex->DrawLatex(x0, y, "invalid hit");
326  pos[1] = y;
327  pos[3] = pos[1] + boxH;
329 }
330 
int numberOfAllHits(HitCategory category) const
Definition: HitPattern.h:804
double getSignedResidual(const FWGeometry *geom, unsigned int id, double resX)
void setTextInfo(const FWModelId &id, const reco::Track *) override
#define REGISTER_FWDETAILVIEW(_classname_, _name_,...)
assert(be >=bs)
void build(const FWModelId &id, const reco::Track *) override
Definition: Electron.h:6
uint16_t getHitPattern(HitCategory category, int position) const
Definition: HitPattern.h:537
#define fwLog(_level_)
Definition: fwLog.h:45
void prepareData(const FWModelId &id, const reco::Track *)
static const char * m_det_tracker_str[]
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
static void drawCanvasBox(Double_t *pos, Color_t fillCol, Int_t fillType=0, bool bg=kTRUE)