Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:53

0001 // ROOT includes
0002 #include "TGLFontManager.h"
0003 #include "TEveScene.h"
0004 #include "TEveManager.h"
0005 #include "TEveStraightLineSet.h"
0006 #include "TEveTrack.h"
0007 #include "TEveTrackPropagator.h"
0008 #include "TEveTrans.h"
0009 #include "TEveText.h"
0010 #include "TEveGeoShape.h"
0011 #include "TGSlider.h"
0012 #include "TGButton.h"
0013 #include "TGLabel.h"
0014 #include "TGLViewer.h"
0015 #include "TCanvas.h"
0016 #include "TLatex.h"
0017 #include "TLegend.h"
0018 
0019 // CMSSW includes
0020 #include "DataFormats/SiStripDetId/interface/SiStripDetId.h"
0021 #include "DataFormats/TrackReco/interface/Track.h"
0022 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
0023 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
0024 
0025 // Fireworks includes
0026 #include "Fireworks/Core/interface/FWModelId.h"
0027 #include "Fireworks/Core/interface/FWColorManager.h"
0028 #include "Fireworks/Core/interface/FWGeometry.h"
0029 #include "Fireworks/Core/interface/FWEventItem.h"
0030 #include "Fireworks/Core/interface/CSGAction.h"
0031 #include "Fireworks/Core/interface/FWIntValueListener.h"
0032 #include "Fireworks/Core/interface/FWMagField.h"
0033 #include "Fireworks/Core/interface/fwLog.h"
0034 
0035 #include "Fireworks/Tracks/plugins/FWTrackHitsDetailView.h"
0036 #include "Fireworks/Tracks/interface/TrackUtils.h"
0037 
0038 #include <functional>
0039 
0040 FWTrackHitsDetailView::FWTrackHitsDetailView()
0041     : m_modules(nullptr),
0042       m_moduleLabels(nullptr),
0043       m_hits(nullptr),
0044       m_slider(nullptr),
0045       m_sliderListener(),
0046       m_legend(nullptr) {}
0047 
0048 FWTrackHitsDetailView::~FWTrackHitsDetailView() {}
0049 
0050 void FWTrackHitsDetailView::build(const FWModelId& id, const reco::Track* track) {
0051   {
0052     TGCompositeFrame* f = new TGVerticalFrame(m_guiFrame);
0053     m_guiFrame->AddFrame(f);
0054     f->AddFrame(new TGLabel(f, "Module Transparency:"), new TGLayoutHints(kLHintsLeft, 2, 2, 0, 0));
0055     m_slider = new TGHSlider(f, 120, kSlider1 | kScaleNo);
0056     f->AddFrame(m_slider, new TGLayoutHints(kLHintsTop | kLHintsLeft, 2, 2, 1, 4));
0057     m_slider->SetRange(0, 100);
0058     m_slider->SetPosition(75);
0059 
0060     m_sliderListener = new FWIntValueListener();
0061     TQObject::Connect(
0062         m_slider, "PositionChanged(Int_t)", "FWIntValueListenerBase", m_sliderListener, "setValue(Int_t)");
0063     m_sliderListener->valueChanged_.connect(
0064         std::bind(&FWTrackHitsDetailView::transparencyChanged, this, std::placeholders::_1));
0065   }
0066 
0067   {
0068     CSGAction* action = new CSGAction(this, "Show Module Labels");
0069     TGCheckButton* b = new TGCheckButton(m_guiFrame, "Show Module Labels");
0070     b->SetState(kButtonUp, false);
0071     m_guiFrame->AddFrame(b, new TGLayoutHints(kLHintsNormal, 2, 3, 1, 4));
0072     TQObject::Connect(b, "Clicked()", "CSGAction", action, "activate()");
0073     action->activated.connect(sigc::mem_fun(*this, &FWTrackHitsDetailView::rnrLabels));
0074   }
0075   {
0076     CSGAction* action = new CSGAction(this, " Pick Camera Center ");
0077     action->createTextButton(m_guiFrame, new TGLayoutHints(kLHintsNormal, 2, 0, 1, 4));
0078     action->setToolTip("Click on object in viewer to set camera center.");
0079     action->activated.connect(sigc::mem_fun(*this, &FWTrackHitsDetailView::pickCameraCenter));
0080   }
0081   makeLegend();
0082 
0083   TGCompositeFrame* p = (TGCompositeFrame*)m_guiFrame->GetParent();
0084   p->MapSubwindows();
0085   p->Layout();
0086 
0087   m_modules = new TEveElementList("Modules");
0088   m_eveScene->AddElement(m_modules);
0089   m_moduleLabels = new TEveElementList("Modules");
0090   m_eveScene->AddElement(m_moduleLabels);
0091   m_hits = new TEveElementList("Hits");
0092   m_eveScene->AddElement(m_hits);
0093   if (track->extra().isAvailable()) {
0094     addModules(*track, id.item(), m_modules, true);
0095     addHits(*track, id.item(), m_hits, true);
0096   }
0097   for (TEveElement::List_i i = m_modules->BeginChildren(), end = m_modules->EndChildren(); i != end; ++i) {
0098     TEveGeoShape* gs = dynamic_cast<TEveGeoShape*>(*i);
0099     const auto& rhs = *(*(i));
0100     if (gs == nullptr && (*i != nullptr)) {
0101       std::cerr << "Got a " << typeid(rhs).name() << ", expecting TEveGeoShape. ignoring (it must be the clusters)."
0102                 << std::endl;
0103       continue;
0104     }
0105     gs->SetMainTransparency(75);
0106     gs->SetPickable(kFALSE);
0107 
0108     TString name = gs->GetElementTitle();
0109     if (!name.Contains("BAD") && !name.Contains("INACTIVE") && !name.Contains("LOST")) {
0110       gs->SetMainColor(kBlue);
0111     }
0112     TEveText* text = new TEveText(name.Data());
0113     text->PtrMainTrans()->SetFrom(gs->RefMainTrans().Array());
0114     text->SetFontMode(TGLFont::kPixmap);
0115     text->SetFontSize(12);
0116     m_moduleLabels->AddElement(text);
0117   }
0118   m_moduleLabels->SetRnrChildren(false);
0119 
0120   TEveTrackPropagator* prop = new TEveTrackPropagator();
0121   prop->SetMagFieldObj(item()->context().getField(), false);
0122   prop->SetStepper(TEveTrackPropagator::kRungeKutta);
0123   prop->SetMaxR(123);
0124   prop->SetMaxZ(300);
0125   prop->SetMaxStep(1);
0126   TEveTrack* trk = fireworks::prepareTrack(*track, prop);
0127   trk->MakeTrack();
0128   trk->SetLineWidth(2);
0129   trk->SetTitle("Track and its ref states");
0130   prop->SetRnrDaughters(kTRUE);
0131   prop->SetRnrReferences(kTRUE);
0132   prop->SetRnrDecay(kTRUE);
0133   prop->SetRnrFV(kTRUE);
0134   trk->SetMainColor(id.item()->defaultDisplayProperties().color());
0135   m_eveScene->AddElement(trk);
0136 
0137   viewerGL()->SetStyle(TGLRnrCtx::kOutline);
0138   viewerGL()->SetDrawCameraCenter(kTRUE);
0139   viewerGL()->ResetCamerasAfterNextUpdate();
0140   viewerGL()->UpdateScene(kFALSE);
0141   gEve->Redraw3D();
0142 
0143   setTextInfo(id, track);
0144 }
0145 
0146 void FWTrackHitsDetailView::setBackgroundColor(Color_t col) {
0147   // Callback for cmsShow change of background
0148 
0149   FWColorManager::setColorSetViewer(viewerGL(), col);
0150 
0151   // adopt label colors to background, this should be implemneted in TEveText
0152   if (m_moduleLabels) {
0153     Color_t x = viewerGL()->GetRnrCtx()->ColorSet().Foreground().GetColorIndex();
0154     for (TEveElement::List_i i = m_moduleLabels->BeginChildren(); i != m_moduleLabels->EndChildren(); ++i)
0155       (*i)->SetMainColor(x);
0156   }
0157 }
0158 
0159 void FWTrackHitsDetailView::pickCameraCenter() { viewerGL()->PickCameraCenter(); }
0160 
0161 void FWTrackHitsDetailView::transparencyChanged(int x) {
0162   for (TEveElement::List_i i = m_modules->BeginChildren(); i != m_modules->EndChildren(); ++i) {
0163     (*i)->SetMainTransparency(x);
0164   }
0165   gEve->Redraw3D();
0166 }
0167 
0168 void FWTrackHitsDetailView::setTextInfo(const FWModelId& id, const reco::Track* track) {
0169   m_infoCanvas->cd();
0170 
0171   float_t x = 0.02;
0172   float y = 0.95;
0173 
0174   TLatex* latex = new TLatex(x, y, "");
0175   const double textsize(0.07);
0176   latex->SetTextSize(2 * textsize);
0177 
0178   latex->DrawLatex(x, y, id.item()->modelName(id.index()).c_str());
0179   y -= latex->GetTextSize() * 0.6;
0180 
0181   latex->SetTextSize(textsize);
0182   float lineH = latex->GetTextSize() * 0.6;
0183 
0184   latex->DrawLatex(
0185       x, y, Form(" P_{T} = %.1f GeV, #eta = %0.2f, #varphi = %0.2f", track->pt(), track->eta(), track->phi()));
0186   y -= lineH;
0187 
0188   if (track->charge() > 0)
0189     latex->DrawLatex(x, y, " charge = +1");
0190   else
0191     latex->DrawLatex(x, y, " charge = -1");
0192   y -= lineH;
0193   y -= lineH;
0194 
0195   latex->DrawLatex(x, y, "Track modules:");
0196   y -= lineH;
0197 
0198   Double_t pos[4];
0199   pos[0] = x + 0.05;
0200   pos[2] = x + 0.20;
0201   Double_t boxH = 0.25 * textsize;
0202 
0203   pos[1] = y;
0204   pos[3] = pos[1] + boxH;
0205   FWDetailViewBase::drawCanvasBox(pos, kBlue);
0206   latex->DrawLatex(x + 0.25, y, "Module");
0207   y -= lineH;
0208 
0209   pos[1] = y;
0210   pos[3] = pos[1] + boxH;
0211   FWDetailViewBase::drawCanvasBox(pos, kRed);
0212   latex->DrawLatex(x + 0.25, y, "LOST Module");
0213   y -= lineH;
0214 
0215   pos[1] = y;
0216   pos[3] = pos[1] + boxH;
0217   FWDetailViewBase::drawCanvasBox(pos, 28);
0218   latex->DrawLatex(x + 0.25, y, "INACTIVE Module");
0219   y -= lineH;
0220 
0221   pos[1] = y;
0222   pos[3] = pos[1] + boxH;
0223   FWDetailViewBase::drawCanvasBox(pos, 218);
0224   latex->DrawLatex(x + 0.25, y, "BAD Module");
0225   y -= lineH;
0226 
0227   Float_t r = 0.01;
0228   Float_t r2 = 0.02;
0229   y -= lineH;
0230   drawCanvasDot(x + r2, y, r2, kGreen);
0231   y -= r;
0232   latex->DrawLatex(x + 3 * r2, y, "Pixel Hits");
0233   y -= lineH;
0234 
0235   drawCanvasDot(x + r2, y, r2, kRed);
0236   y -= r;
0237   latex->DrawLatex(x + 3 * r2, y, "Extra Pixel Hits");
0238   y -= lineH;
0239 
0240   m_legend->SetY2(y);
0241   m_legend->Draw();
0242   m_legend = nullptr;  // Deleted together with TPad.
0243 }
0244 
0245 void FWTrackHitsDetailView::makeLegend(void) {
0246   m_legend = new TLegend(0.01, 0.01, 0.99, 0.99, nullptr, "NDC");
0247   m_legend->SetFillColor(kWhite);
0248   m_legend->SetTextSize(0.07);
0249   m_legend->SetBorderSize(0);
0250   m_legend->SetMargin(0.15);
0251   m_legend->SetEntrySeparation(0.01);
0252 
0253   TEveStraightLineSet* legend = new TEveStraightLineSet("siStripCluster");
0254   legend->SetLineWidth(3);
0255   legend->SetLineColor(kGreen);
0256   m_legend->AddEntry(legend, "Exact SiStripCluster", "l");
0257 
0258   TEveStraightLineSet* legend2 = new TEveStraightLineSet("siStripCluster2");
0259   legend2->SetLineWidth(3);
0260   legend2->SetLineColor(kRed);
0261   m_legend->AddEntry(legend2, "Extra SiStripCluster", "l");
0262 
0263   TEveStraightLineSet* legend3 = new TEveStraightLineSet("refStates");
0264   legend3->SetDepthTest(kFALSE);
0265   legend3->SetMarkerColor(kYellow);
0266   legend3->SetMarkerStyle(kPlus);
0267   legend3->SetMarkerSize(2);
0268   m_legend->AddEntry(legend3, "Inner/Outermost States", "p");
0269 
0270   TEveStraightLineSet* legend4 = new TEveStraightLineSet("vertex");
0271   legend4->SetDepthTest(kFALSE);
0272   legend4->SetMarkerColor(kRed);
0273   legend4->SetMarkerStyle(kFullDotLarge);
0274   legend4->SetMarkerSize(2);
0275   m_legend->AddEntry(legend4, "Vertex", "p");
0276 
0277   TEveStraightLineSet* legend5 = new TEveStraightLineSet("cameraCenter");
0278   legend5->SetDepthTest(kFALSE);
0279   legend5->SetMarkerColor(kCyan);
0280   legend5->SetMarkerStyle(kFullDotLarge);
0281   legend5->SetMarkerSize(2);
0282   m_legend->AddEntry(legend5, "Camera center", "p");
0283 }
0284 
0285 void FWTrackHitsDetailView::addTrackerHits3D(std::vector<TVector3>& points,
0286                                              class TEveElementList* tList,
0287                                              Color_t color,
0288                                              int size) {
0289   // !AT this is  detail view specific, should move to track hits
0290   // detail view
0291 
0292   TEvePointSet* pointSet = new TEvePointSet();
0293   pointSet->SetMarkerSize(size);
0294   pointSet->SetMarkerStyle(4);
0295   pointSet->SetPickable(kTRUE);
0296   pointSet->SetTitle("Pixel Hits");
0297   pointSet->SetMarkerColor(color);
0298 
0299   for (std::vector<TVector3>::const_iterator it = points.begin(), itEnd = points.end(); it != itEnd; ++it) {
0300     pointSet->SetNextPoint(it->x(), it->y(), it->z());
0301   }
0302   tList->AddElement(pointSet);
0303 }
0304 
0305 void FWTrackHitsDetailView::addHits(const reco::Track& track,
0306                                     const FWEventItem* iItem,
0307                                     TEveElement* trkList,
0308                                     bool addNearbyHits) {
0309   std::vector<TVector3> pixelPoints;
0310   fireworks::pushPixelHits(pixelPoints, *iItem, track);
0311   TEveElementList* pixels = new TEveElementList("Pixels");
0312   trkList->AddElement(pixels);
0313   if (addNearbyHits) {
0314     // get the extra hits
0315     std::vector<TVector3> pixelExtraPoints;
0316     fireworks::pushNearbyPixelHits(pixelExtraPoints, *iItem, track);
0317     // draw first the others
0318     addTrackerHits3D(pixelExtraPoints, pixels, kRed, 1);
0319     // then the good ones, so they're on top
0320     addTrackerHits3D(pixelPoints, pixels, kGreen, 1);
0321   } else {
0322     // just add those points with the default color
0323     addTrackerHits3D(pixelPoints, pixels, iItem->defaultDisplayProperties().color(), 1);
0324   }
0325 
0326   // strips
0327   TEveElementList* strips = new TEveElementList("Strips");
0328   trkList->AddElement(strips);
0329   fireworks::addSiStripClusters(iItem, track, strips, addNearbyHits, false);
0330 }
0331 
0332 //______________________________________________________________________________
0333 
0334 void FWTrackHitsDetailView::addModules(const reco::Track& track,
0335                                        const FWEventItem* iItem,
0336                                        TEveElement* trkList,
0337                                        bool addLostHits) {
0338   std::set<unsigned int> ids;
0339   for (trackingRecHit_iterator recIt = track.recHitsBegin(), recItEnd = track.recHitsEnd(); recIt != recItEnd;
0340        ++recIt) {
0341     DetId detid = (*recIt)->geographicalId();
0342     if (!addLostHits && !(*recIt)->isValid())
0343       continue;
0344     if (detid.rawId() != 0) {
0345       TString name("");
0346       switch (detid.det()) {
0347         case DetId::Tracker:
0348           name = iItem->getGeom()->getTrackerTopology()->print(detid);
0349           break;
0350 
0351         case DetId::Muon:
0352           switch (detid.subdetId()) {
0353             case MuonSubdetId::DT:
0354               name = "DT";
0355               detid = DetId(DTChamberId(detid));  // get rid of layer bits
0356               break;
0357             case MuonSubdetId::CSC:
0358               name = "CSC";
0359               break;
0360             case MuonSubdetId::RPC:
0361               name = "RPC";
0362               break;
0363             case MuonSubdetId::GEM:
0364               name = "GEM";
0365               break;
0366             case MuonSubdetId::ME0:
0367               name = "ME0";
0368               break;
0369             default:
0370               break;
0371           }
0372           break;
0373         default:
0374           break;
0375       }
0376       if (!ids.insert(detid.rawId()).second)
0377         continue;
0378       if (iItem->getGeom()) {
0379         TEveGeoShape* shape = iItem->getGeom()->getEveShape(detid);
0380         if (nullptr != shape) {
0381           shape->SetMainTransparency(65);
0382           shape->SetPickable(kTRUE);
0383           switch ((*recIt)->type()) {
0384             case TrackingRecHit::valid:
0385               shape->SetMainColor(iItem->defaultDisplayProperties().color());
0386               break;
0387             case TrackingRecHit::missing:
0388             case TrackingRecHit::missing_inner:
0389             case TrackingRecHit::missing_outer:
0390               name += "LOST ";
0391               shape->SetMainColor(kRed);
0392               break;
0393             case TrackingRecHit::inactive_inner:
0394             case TrackingRecHit::inactive_outer:
0395             case TrackingRecHit::inactive:
0396               name += "INACTIVE ";
0397               shape->SetMainColor(28);
0398               break;
0399             case TrackingRecHit::bad:
0400               name += "BAD ";
0401               shape->SetMainColor(218);
0402               break;
0403           }
0404           shape->SetTitle(name + ULong_t(detid.rawId()));
0405           trkList->AddElement(shape);
0406         } else {
0407           fwLog(fwlog::kInfo) << "Failed to get shape extract for a tracking rec hit: "
0408                               << "\n"
0409                               << fireworks::info(detid) << std::endl;
0410         }
0411       }
0412     }
0413   }
0414 }
0415 
0416 void FWTrackHitsDetailView::rnrLabels() {
0417   m_moduleLabels->SetRnrChildren(!m_moduleLabels->GetRnrChildren());
0418   gEve->Redraw3D();
0419 }
0420 
0421 REGISTER_FWDETAILVIEW(FWTrackHitsDetailView, Hits);