CMS 3D CMS Logo

FWPFCandidateDetailView.cc
Go to the documentation of this file.
1 // ROOT includes
2 #include "TEveScene.h"
3 #include "TEveManager.h"
4 #include "TEveStraightLineSet.h"
5 #include "TEveTrack.h"
6 #include "TEveTrackPropagator.h"
7 #include "TEveTrans.h"
8 #include "TEveText.h"
9 #include "TEveGeoShape.h"
10 #include "TGLViewer.h"
11 #include "TGLScenePad.h"
12 #include "TCanvas.h"
13 #include "TLatex.h"
14 #include "TLegend.h"
15 
16 #include "TH2.h"
17 
18 #include "TAxis.h"
19 #include "TGSlider.h"
20 #include "TGButton.h"
21 #include "TGLabel.h"
22 #include "TGLCameraOverlay.h"
23 
26 
36 
38 
45 
46 #include <functional>
47 
49  : m_range(1),
50  m_candidate(nullptr),
51  m_legend(nullptr),
52  m_slider(nullptr),
53  m_sliderListener(),
54  m_eventList(nullptr),
55  m_plotEt(true),
56  m_rnrHcal(true) {}
57 
59 
61 
63 
65  float dx = m_candidate->eta() - x;
66  float dy = m_candidate->phi() - y;
67  float sd = TMath::Sqrt(dx * dx + dy * dy);
68  return sd < m_range;
69 }
70 
71 //______________________________________________________________________________
72 
74  m_legend = new TLegend(0.01, 0.01, 0.99, 0.99, nullptr, "NDC");
75  m_legend->SetFillColor(kWhite);
76  m_legend->SetTextSize(0.07);
77  m_legend->SetBorderSize(0);
78  m_legend->SetMargin(0.15);
79  m_legend->SetEntrySeparation(0.01);
80 }
81 
82 //______________________________________________________________________________
83 
85  static float kSliderRangeFactor = 0.2;
86 
87  m_range = x * kSliderRangeFactor;
88 
89  if (m_eventList)
91 
92  gEve->Redraw3D();
93 }
94 
95 //______________________________________________________________________________
96 
98  m_infoCanvas->cd();
99 
100  float_t x = 0.02;
101  float y = 0.95;
102 
103  TLatex* latex = new TLatex(x, y, "");
104  const double textsize(0.07);
105  latex->SetTextSize(textsize);
106 
107  latex->DrawLatex(x, y, id.item()->modelName(id.index()).c_str());
108  y -= latex->GetTextSize() * 0.6;
109 
110  latex->SetTextSize(textsize);
111  float lineH = latex->GetTextSize() * 0.6;
112 
113  latex->DrawLatex(
114  x, y, Form(" P_{T} = %.1f GeV, #eta = %0.2f, #varphi = %0.2f", track->pt(), track->eta(), track->phi()));
115  y -= lineH;
116 
117  if (track->charge() > 0)
118  latex->DrawLatex(x, y, " charge = +1");
119  else
120  latex->DrawLatex(x, y, " charge = -1");
121  y -= lineH;
122  y -= lineH;
123 
124  m_legend->SetY2(y);
125  m_legend->Draw();
126  m_legend = nullptr; // Deleted together with TPad.
127 }
128 
130  printf("plotEt = %d \n", m_plotEt);
131  m_plotEt = !m_plotEt;
133 }
134 
136  printf("rnrHcal = %d \n", m_rnrHcal);
137  m_rnrHcal = !m_rnrHcal;
139 }
140 
141 //______________________________________________________________________________
142 
143 void FWPFCandidateDetailView::build(const FWModelId& id, const reco::PFCandidate* candidate) {
144  m_candidate = candidate;
145 
146  // ROOT GUI
147  //
148  {
149  TGCompositeFrame* f = new TGVerticalFrame(m_guiFrame);
150  m_guiFrame->AddFrame(f);
151  f->AddFrame(new TGLabel(f, "Rng:"), new TGLayoutHints(kLHintsLeft, 2, 2, 0, 0));
152  m_slider = new TGHSlider(f, 120, kSlider1 | kScaleNo);
153  f->AddFrame(m_slider, new TGLayoutHints(kLHintsTop | kLHintsLeft, 2, 2, 1, 4));
154  m_slider->SetRange(1, 50);
155  m_slider->SetPosition(8);
156 
158  TQObject::Connect(
159  m_slider, "PositionChanged(Int_t)", "FWIntValueListenerBase", m_sliderListener, "setValue(Int_t)");
161  std::bind(&FWPFCandidateDetailView::rangeChanged, this, std::placeholders::_1));
162  {
163  CSGAction* action = new CSGAction(this, "Scale Et");
164  TGCheckButton* b = new TGCheckButton(m_guiFrame, action->getName().c_str());
165  b->SetState(kButtonDown, true);
166  m_guiFrame->AddFrame(b, new TGLayoutHints(kLHintsNormal, 2, 3, 1, 4));
167  TQObject::Connect(b, "Clicked()", "CSGAction", action, "activate()");
168  action->activated.connect(sigc::mem_fun(*this, &FWPFCandidateDetailView::plotEtChanged));
169  }
170  {
171  CSGAction* action = new CSGAction(this, "RnrHcal");
172  TGCheckButton* b = new TGCheckButton(m_guiFrame, action->getName().c_str());
173  b->SetState(kButtonDown, true);
174  m_guiFrame->AddFrame(b, new TGLayoutHints(kLHintsNormal, 2, 3, 1, 4));
175  TQObject::Connect(b, "Clicked()", "CSGAction", action, "activate()");
176  action->activated.connect(sigc::mem_fun(*this, &FWPFCandidateDetailView::rnrHcalChanged));
177  }
178  }
179  makeLegend();
180  setTextInfo(id, candidate);
181 
182  TGCompositeFrame* p = (TGCompositeFrame*)m_guiFrame->GetParent();
183  p->MapSubwindows();
184  p->Layout();
185 
187  // GL stuff
188 
189  m_candidate = candidate;
190 
191  try {
194  event->getByLabel(edm::InputTag("particleFlowRecHitECAL"), ecalH);
195  if (ecalH.product())
196  voteMaxEtEVal(ecalH.product());
197 
199  event->getByLabel(edm::InputTag("particleFlowRecHitHBHEHO"), hcalH);
200  if (hcalH.product())
201  voteMaxEtEVal(hcalH.product());
202  } catch (const cms::Exception& iE) {
203  std::cerr << iE.what();
204  }
205 
206  m_eveScene->GetGLScene()->SetSelectable(false);
207  m_eventList = new TEveElementList("PFDetailView");
208  m_eveScene->AddElement(m_eventList);
209 
210  viewerGL()->SetStyle(TGLRnrCtx::kOutline);
211  viewerGL()->SetCurrentCamera(TGLViewer::kCameraOrthoXOY);
212 
213  TGLCameraOverlay* co = viewerGL()->GetCameraOverlay();
214  co->SetShowOrthographic(kTRUE);
215  co->SetOrthographicMode(TGLCameraOverlay::kAxis);
216 
217  viewerGL()->ResetCamerasAfterNextUpdate();
218  try {
220  } catch (...) {
221  printf("unknown exception \n");
222  }
223 
224  viewerGL()->UpdateScene(kFALSE);
225 
226  gEve->Redraw3D();
227 
228  // gEve->AddToListTree(m_eventList, true);//debug, used with --eve option
229 }
230 
231 //______________________________________________________________________________
232 
233 void FWPFCandidateDetailView::voteMaxEtEVal(const std::vector<reco::PFRecHit>* hits) {
234  if (!hits)
235  return;
236 
237  // FIXME: require access to geometry while reading from reco file
238  if ((!hits->empty()) && hits->front().hasCaloCell())
239  for (std::vector<reco::PFRecHit>::const_iterator it = hits->begin(); it != hits->end(); ++it) {
240  TEveVector centre(it->position().x(), it->position().y(), it->position().z());
241  float E = it->energy();
242  float Et = FWPFMaths::calculateEt(centre, E);
243  item()->context().voteMaxEtAndEnergy(Et, E);
244  }
245 }
246 
247 //______________________________________________________________________________
248 
249 void FWPFCandidateDetailView::addTracks(const std::vector<reco::PFRecTrack>* tracks) {
250  for (std::vector<reco::PFRecTrack>::const_iterator it = tracks->begin(); it != tracks->end(); ++it) {
252  /*
253  if (!isPntInRng(it->trackRef().innerMomentum().Eta(), it->position().Phi()))
254  continue;
255 
256  TEveLine* line = new TEveLine("Track");
257  line->SetMainColor(kYellow);
258  int N = it->nTrajectoryPoints();
259 
260  for (int p = 0 ; p<N; ++p) {
261  pos = track.extrapolatedPoint(p).position();
262 
263  if( pos.Eta() !=0 and pos.Phi() !=0)
264  line->SetNextPoint(pos.Eta(), pos.Phi(), 0);
265  }
266  m_eventList->AddElement(line);
267  */
268  }
269 }
270 
271 //______________________________________________________________________________
272 
273 void FWPFCandidateDetailView::addClusters(const std::vector<reco::PFCluster>* cluster) {
274  if (!cluster)
275  return;
276 
277  Color_t col = kViolet + 9;
278 
279  TEveStraightLineSet* ls = new TEveStraightLineSet("cluster_ls");
280  ls->SetMainColor(col);
281  m_eventList->AddElement(ls);
282 
283  TEvePointSet* ps = new TEvePointSet("cluster_ps");
284  ps->SetMainColor(col);
285  ps->SetMarkerStyle(2);
286  ps->SetMarkerSize(0.005);
287  m_eventList->AddElement(ps);
288 
289  for (std::vector<reco::PFCluster>::const_iterator it = cluster->begin(); it != cluster->end(); ++it) {
290  if (!isPntInRng(it->position().Eta(), it->position().Phi()))
291  continue;
292 
293  ps->SetNextPoint(it->position().Eta(), it->position().Phi(), 0);
294 
295  /*
296  const std::vector< reco::PFRecHitFraction >& fractions = it->recHitFractions();
297  for (std::vector< reco::PFRecHitFraction >::const_iterator fi = fractions.begin(); fi != fractions.end(); ++fi)
298  {
299  // !!! AMT can't get fi->recHitRef().position()
300  // ls->AddLine(it->position().Eta(), it->position().Phi(), 0,
301  // fi->recHitRef().position().Eta(), fi->recHitRef().position().Phi(), 0);
302  }
303  */
304  }
305 }
306 namespace {
307  void WrapTwoPi(std::vector<TEveVector>& hc, float y) {
308  if (TMath::Abs(hc[0].fY) < 2)
309  return;
310 
311  if (hc[0].fY > 0 && hc[1].fY > 0 && hc[2].fY > 0 && hc[3].fY > 0)
312  return;
313  if (hc[0].fY < 0 && hc[1].fY < 0 && hc[2].fY < 0 && hc[3].fY < 0)
314  return;
315 
316  for (int i = 0; i < 4; ++i)
317  if (y > 0 && hc[i].fY < 0)
318  hc[i].fY += TMath::TwoPi();
319  else if (y < 0 && hc[i].fY > 0)
320  hc[i].fY -= TMath::TwoPi();
321  }
322 } // namespace
323 //______________________________________________________________________________
324 namespace {
325  TEveStraightLineSet::Line_t* AddLineToLineSet(TEveStraightLineSet* ls,
326  const std::vector<TEveVector>& pnts,
327  int i0,
328  int i1) {
329  if (false) {
330  printf("add line \n");
331  pnts[i0].Dump();
332  pnts[i1].Dump();
333  }
334  return ls->AddLine(pnts[i0], pnts[i1]);
335  // return ls->AddLine(pnts[i0].Eta(),pnts[i0].Phi(), 0 , pnts[i1].Eta(),pnts[i1].Phi(), 0);
336  }
337 } // namespace
338 void FWPFCandidateDetailView::addHits(const std::vector<reco::PFRecHit>* hits) {
339  TEveStraightLineSet* lsOutline = (TEveStraightLineSet*)m_eventList->FindChild("outlines");
340 
341  TEvePointSet* ps = new TEvePointSet("test");
342  m_eventList->AddElement(ps);
343  ps->SetMainColor(kOrange);
344 
345  // FIXME, requires access to geometry
346  if ((!hits->empty()) && hits->front().hasCaloCell())
347  for (std::vector<reco::PFRecHit>::const_iterator it = hits->begin(); it != hits->end(); ++it) {
348  const auto& corners = it->getCornersXYZ();
349  if (!isPntInRng(corners[0].eta(), corners[0].phi()))
350  continue;
351 
352  std::vector<TEveVector> hc;
353  hc.reserve(4);
354  for (int k = 0; k < 4; ++k) {
355  hc.push_back(TEveVector(corners[k].eta(), corners[k].phi(), 0));
356  // ps->SetNextPoint(corners[k].eta(),corners[k].phi(),0 ); //debug
357  }
358 
359  WrapTwoPi(hc, corners[0].phi());
360 
361  AddLineToLineSet(lsOutline, hc, 0, 1);
362  AddLineToLineSet(lsOutline, hc, 1, 2);
363  AddLineToLineSet(lsOutline, hc, 2, 3);
364  AddLineToLineSet(lsOutline, hc, 3, 0);
365 
366  // get scaled corners
367  TEveVector centerOfGravity = hc[0] + hc[1] + hc[2] + hc[3];
368  centerOfGravity *= 0.25;
369 
370  std::vector<TEveVector> radialVectors;
371  radialVectors.reserve(4);
372  for (int k = 0; k < 4; ++k)
373  radialVectors.push_back(TEveVector(hc[k] - centerOfGravity));
374 
375  float factor = 1;
376  if (m_plotEt) {
377  float Et = FWPFMaths::calculateEt(TEveVector(corners[0].x(), corners[0].y(), corners[0].z()), it->energy());
379  } else
380  factor = it->energy() / context().getMaxEnergyInEvent(false);
381 
382  std::vector<TEveVector> scaledCorners;
383  for (int k = 0; k < 4; ++k) {
384  radialVectors[k] *= factor;
385  scaledCorners.push_back(TEveVector(radialVectors[k] + centerOfGravity));
386  }
387 
388  TEveStraightLineSet* ls = (TEveStraightLineSet*)m_eventList->FindChild(Form("%d_rechit", it->depth()));
389  AddLineToLineSet(ls, scaledCorners, 0, 1);
390  AddLineToLineSet(ls, scaledCorners, 1, 2);
391  AddLineToLineSet(ls, scaledCorners, 2, 3);
392  // AddLineToLineSet(ls, scaledCorners, 3, 0);
393  TEveStraightLineSet::Line_t* li = AddLineToLineSet(ls, scaledCorners, 3, 0);
394  ls->AddMarker(centerOfGravity, li->fId);
395  }
396 }
397 
398 //______________________________________________________________________________
399 
401  if (m_eventList->HasChildren())
402  m_eventList->DestroyElements();
403 
404  for (int depth = 0; depth < 6; ++depth) {
405  TEveStraightLineSet* ls = new TEveStraightLineSet(Form("%d_rechit", depth));
406 
407  if (depth == 0)
408  ls->SetLineColor(kGray);
409  else if (depth == 1)
410  ls->SetLineColor(kRed);
411  else if (depth == 2)
412  ls->SetLineColor(kGreen);
413  else if (depth == 3)
414  ls->SetLineColor(kMagenta);
415  else if (depth == 4)
416  ls->SetLineColor(kOrange);
417  else if (depth == 5)
418  ls->SetLineColor(kYellow);
419 
420  ls->SetMarkerStyle(1);
421  m_eventList->AddElement(ls);
422  }
423 
424  TEveStraightLineSet* ls = new TEveStraightLineSet("outlines");
425  ls->SetLineColor(kGray);
426  ls->SetMainTransparency(80);
427  m_eventList->AddElement(ls);
428 
430 
431  //
432  // recHits
433  //
434  try {
436  event->getByLabel(edm::InputTag("particleFlowRecHitECAL"), ecalH);
437  addHits(ecalH.product());
438  } catch (const cms::Exception& iE) {
439  std::cerr << iE.what();
440  }
441 
442  if (m_rnrHcal) {
443  try {
445  event->getByLabel(edm::InputTag("particleFlowRecHitHF"), ecalH);
446  addHits(ecalH.product());
447  } catch (const cms::Exception& iE) {
448  std::cerr << iE.what();
449  }
450 
451  try {
453  event->getByLabel(edm::InputTag("particleFlowRecHitHBHEHO"), hcalH);
454  addHits(hcalH.product());
455  } catch (const cms::Exception& iE) {
456  std::cerr << iE.what();
457  }
458  }
459 
460  //
461  // clusters
462  //
463  try {
465  event->getByLabel(edm::InputTag("particleFlowClusterECAL"), ecalClustersH);
466  addClusters(ecalClustersH.product());
467  } catch (const cms::Exception& iE) {
468  std::cerr << iE.what();
469  }
470 
471  //
472  // tracks
473  //
474  try {
476  event->getByLabel(edm::InputTag("pfTrack"), trackH);
477  addTracks(trackH.product());
478  } catch (const cms::Exception& iE) {
479  std::cerr << iE.what();
480  }
481 }
482 
484  PF Candidate,
const double TwoPi
bool isPntInRng(float x, float y)
FWIntValueListener * m_sliderListener
sigc::signal< void(Int_t)> valueChanged_
const FWEventItem * item()
T const * product() const
Definition: Handle.h:70
float getMaxEnergyInEvent(bool isEt) const
Definition: Context.cc:181
void setTextInfo(const FWModelId &id, const reco::PFCandidate *) override
const fireworks::Context & context() const
#define REGISTER_FWDETAILVIEW(_classname_, _name_,...)
__host__ __device__ VT * co
Definition: prefixScan.h:47
const fireworks::Context & context() const
Definition: FWEventItem.h:121
const edm::EventBase * getCurrentEvent() const
void voteMaxEtAndEnergy(float Et, float energy) const
Definition: Context.cc:169
double f[11][100]
float calculateEt(const TEveVector &centre, float e)
Definition: FWPFMaths.cc:115
void voteMaxEtEVal(const std::vector< reco::PFRecHit > *hits)
def ls(path, rec=False)
Definition: eostools.py:349
static FWGUIManager * getGUIManager()
const reco::PFCandidate * m_candidate
double b
Definition: hdecay.h:118
void addHits(const std::vector< reco::PFRecHit > *)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
void addClusters(const std::vector< reco::PFCluster > *)
col
Definition: cuy.py:1009
float x
char const * what() const noexcept override
Definition: Exception.cc:103
void build(const FWModelId &id, const reco::PFCandidate *) override
double phi() const final
momentum azimuthal angle
void addTracks(const std::vector< reco::PFRecTrack > *)
double eta() const final
momentum pseudorapidity