CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/Fireworks/Tracks/plugins/FWTrackHitsDetailView.cc

Go to the documentation of this file.
00001 // ROOT includes
00002 #include "TGLFontManager.h"
00003 #include "TEveScene.h"
00004 #include "TEveManager.h"
00005 #include "TEveStraightLineSet.h"
00006 #include "TEveTrack.h"
00007 #include "TEveTrackPropagator.h"
00008 #include "TEveTrans.h"
00009 #include "TEveText.h"
00010 #include "TEveGeoShape.h"
00011 #include "TGSlider.h"
00012 #include "TGButton.h"
00013 #include "TGLabel.h"
00014 #include "TGLViewer.h"
00015 #include "TCanvas.h"
00016 #include "TLatex.h"
00017 #include "TLegend.h"
00018 
00019 // CMSSW includes
00020 #include "DataFormats/SiStripDetId/interface/SiStripDetId.h"
00021 #include "DataFormats/TrackReco/interface/Track.h"
00022 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
00023 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
00024 
00025 // Fireworks includes
00026 #include "Fireworks/Core/interface/FWModelId.h"
00027 #include "Fireworks/Core/interface/FWColorManager.h"
00028 #include "Fireworks/Core/interface/FWGeometry.h"
00029 #include "Fireworks/Core/interface/FWEventItem.h"
00030 #include "Fireworks/Core/interface/CSGAction.h"
00031 #include "Fireworks/Core/interface/FWIntValueListener.h"
00032 #include "Fireworks/Core/interface/FWMagField.h"
00033 #include "Fireworks/Core/interface/fwLog.h"
00034 
00035 #include "Fireworks/Tracks/plugins/FWTrackHitsDetailView.h"
00036 #include "Fireworks/Tracks/interface/TrackUtils.h"
00037 
00038 FWTrackHitsDetailView::FWTrackHitsDetailView ():
00039   m_modules(0),
00040   m_moduleLabels(0),
00041   m_hits(0),
00042   m_slider(0),
00043   m_sliderListener(),
00044   m_legend(0)
00045 {}
00046 
00047 FWTrackHitsDetailView::~FWTrackHitsDetailView ()
00048 {
00049 }
00050 
00051 void
00052 FWTrackHitsDetailView::build (const FWModelId &id, const reco::Track* track)
00053 {      
00054    {
00055       TGCompositeFrame* f  = new TGVerticalFrame(m_guiFrame);
00056       m_guiFrame->AddFrame(f);
00057       f->AddFrame(new TGLabel(f, "Module Transparency:"), new TGLayoutHints(kLHintsLeft, 2, 2, 0, 0));
00058       m_slider = new TGHSlider(f, 120, kSlider1 | kScaleNo);
00059       f->AddFrame(m_slider, new TGLayoutHints(kLHintsTop | kLHintsLeft, 2, 2, 1, 4));
00060       m_slider->SetRange(0, 100);
00061       m_slider->SetPosition(75);
00062 
00063       m_sliderListener =  new FWIntValueListener();
00064       TQObject::Connect(m_slider, "PositionChanged(Int_t)", "FWIntValueListenerBase",  m_sliderListener, "setValue(Int_t)");
00065       m_sliderListener->valueChanged_.connect(boost::bind(&FWTrackHitsDetailView::transparencyChanged,this,_1));
00066    }
00067 
00068    {
00069       CSGAction* action = new CSGAction(this, "Show Module Labels");
00070       TGCheckButton* b = new TGCheckButton(m_guiFrame, "Show Module Labels" );
00071       b->SetState(kButtonUp, false);
00072       m_guiFrame->AddFrame(b, new TGLayoutHints( kLHintsNormal, 2, 3, 1, 4));
00073       TQObject::Connect(b, "Clicked()", "CSGAction", action, "activate()");
00074       action->activated.connect(sigc::mem_fun(this, &FWTrackHitsDetailView::rnrLabels));
00075    }
00076    {
00077       CSGAction* action = new CSGAction(this, " Pick Camera Center ");
00078       action->createTextButton(m_guiFrame, new TGLayoutHints( kLHintsNormal, 2, 0, 1, 4));
00079       action->setToolTip("Click on object in viewer to set camera center.");
00080       action->activated.connect(sigc::mem_fun(this, &FWTrackHitsDetailView::pickCameraCenter));
00081    }
00082    makeLegend();
00083    
00084    TGCompositeFrame* p = (TGCompositeFrame*)m_guiFrame->GetParent();
00085    p->MapSubwindows();
00086    p->Layout();
00087 
00088    m_modules = new TEveElementList( "Modules" );
00089    m_eveScene->AddElement( m_modules );
00090    m_moduleLabels = new TEveElementList( "Modules" );
00091    m_eveScene->AddElement( m_moduleLabels );
00092    m_hits = new TEveElementList( "Hits" );
00093    m_eveScene->AddElement( m_hits );
00094    if( track->extra().isAvailable())
00095    {
00096       addModules( *track, id.item(), m_modules, true );
00097       addHits( *track, id.item(), m_hits, true );
00098    }
00099    for( TEveElement::List_i i = m_modules->BeginChildren(), end = m_modules->EndChildren(); i != end; ++i )
00100    {
00101       TEveGeoShape* gs = dynamic_cast<TEveGeoShape*>(*i);
00102       if (gs == 0 && (*i != 0)) {
00103         std::cerr << "Got a " << typeid(**i).name() << ", expecting TEveGeoShape. ignoring (it must be the clusters)." << std::endl;
00104         continue;
00105       }
00106       gs->SetMainTransparency(75);
00107       gs->SetPickable(kFALSE);
00108 
00109       TString name = gs->GetElementTitle();
00110       if (!name.Contains("BAD") && !name.Contains("INACTIVE") && !name.Contains("LOST")) {
00111           gs->SetMainColor(kBlue);
00112       }
00113       TEveText* text = new TEveText(name.Data());
00114       text->PtrMainTrans()->SetFrom(gs->RefMainTrans().Array());
00115       text->SetFontMode(TGLFont::kPixmap);
00116       text->SetFontSize(12);
00117       m_moduleLabels->AddElement(text); 
00118    }
00119    m_moduleLabels->SetRnrChildren(false);
00120 
00121    TEveTrackPropagator* prop = new TEveTrackPropagator();
00122    prop->SetMagFieldObj(item()->context().getField(), false);
00123    prop->SetStepper(TEveTrackPropagator::kRungeKutta);
00124    prop->SetMaxR(123);
00125    prop->SetMaxZ(300);
00126    prop->SetMaxStep(1);
00127    TEveTrack* trk = fireworks::prepareTrack( *track, prop );
00128    trk->MakeTrack();
00129    trk->SetLineWidth(2);
00130    trk->SetTitle( "Track and its ref states" );
00131    prop->SetRnrDaughters(kTRUE);
00132    prop->SetRnrReferences(kTRUE);
00133    prop->SetRnrDecay(kTRUE);
00134    prop->SetRnrFV(kTRUE);
00135    trk->SetMainColor(id.item()->defaultDisplayProperties().color());
00136    m_eveScene->AddElement(trk);
00137 
00138    viewerGL()->SetStyle(TGLRnrCtx::kOutline);
00139    viewerGL()->SetDrawCameraCenter(kTRUE);
00140    viewerGL()->ResetCamerasAfterNextUpdate();
00141    viewerGL()->UpdateScene(kFALSE);
00142    gEve->Redraw3D();
00143 
00144    setTextInfo(id, track);
00145 }
00146 
00147 void
00148 FWTrackHitsDetailView::setBackgroundColor(Color_t col)
00149 {
00150    // Callback for cmsShow change of background
00151 
00152    FWColorManager::setColorSetViewer(viewerGL(), col);
00153 
00154    // adopt label colors to background, this should be implemneted in TEveText
00155    if (m_moduleLabels)
00156    {
00157       Color_t x = viewerGL()->GetRnrCtx()->ColorSet().Foreground().GetColorIndex();
00158       for (TEveElement::List_i i=m_moduleLabels->BeginChildren(); i!=m_moduleLabels->EndChildren(); ++i)
00159          (*i)->SetMainColor(x);
00160    }
00161 }
00162 
00163 void
00164 FWTrackHitsDetailView::pickCameraCenter()
00165 {
00166    viewerGL()->PickCameraCenter();
00167 }
00168 
00169 void
00170 FWTrackHitsDetailView::transparencyChanged(int x)
00171 {
00172    for (TEveElement::List_i i=m_modules->BeginChildren(); i!=m_modules->EndChildren(); ++i)
00173    {
00174       (*i)->SetMainTransparency(x);
00175    }
00176    gEve->Redraw3D();
00177 }
00178 
00179 void
00180 FWTrackHitsDetailView::setTextInfo(const FWModelId &id, const reco::Track* track)
00181 {
00182    m_infoCanvas->cd();
00183 
00184    float_t x  = 0.02;
00185    float   y  = 0.95;
00186 
00187    TLatex* latex = new TLatex( x, y, "" );
00188    const double textsize( 0.07 );
00189    latex->SetTextSize( 2*textsize );
00190 
00191    latex->DrawLatex( x, y, id.item()->modelName( id.index()).c_str());
00192    y -= latex->GetTextSize()*0.6;
00193 
00194    latex->SetTextSize( textsize );
00195    float lineH = latex->GetTextSize()*0.6;
00196 
00197    latex->DrawLatex( x, y, Form( " P_{T} = %.1f GeV, #eta = %0.2f, #varphi = %0.2f",
00198                                  track->pt(), track->eta(), track->phi()));
00199    y -= lineH;
00200 
00201    if( track->charge() > 0 )
00202       latex->DrawLatex( x, y, " charge = +1" );
00203    else
00204       latex->DrawLatex( x, y, " charge = -1" );
00205    y -= lineH;
00206    y -= lineH;
00207 
00208    latex->DrawLatex( x, y, "Track modules:");
00209    y -= lineH;
00210 
00211    Double_t pos[4];
00212    pos[0] = x+0.05;
00213    pos[2] = x+0.20;
00214    Double_t boxH = 0.25*textsize;
00215 
00216    pos[1] = y; pos[3] = pos[1] + boxH;
00217    FWDetailViewBase::drawCanvasBox( pos, kBlue );
00218    latex->DrawLatex( x + 0.25, y, "Module" );
00219    y -= lineH;
00220 
00221    pos[1] = y; pos[3] = pos[1] + boxH;
00222    FWDetailViewBase::drawCanvasBox( pos, kRed );
00223    latex->DrawLatex( x + 0.25, y, "LOST Module" );
00224    y -= lineH;
00225 
00226    pos[1] = y; pos[3] = pos[1] + boxH;
00227    FWDetailViewBase::drawCanvasBox( pos, 28 );
00228    latex->DrawLatex( x + 0.25, y, "INACTIVE Module" );
00229    y -= lineH;
00230 
00231    pos[1] = y; pos[3] = pos[1] + boxH;
00232    FWDetailViewBase::drawCanvasBox( pos, 218 );
00233    latex->DrawLatex( x + 0.25, y, "BAD Module" );
00234    y -= lineH;
00235 
00236    Float_t r = 0.01;
00237    Float_t r2 = 0.02;
00238    y -= lineH;
00239    drawCanvasDot( x + r2, y, r2, kGreen );
00240    y -= r;
00241    latex->DrawLatex( x + 3 * r2, y, "Pixel Hits" );
00242    y -= lineH;
00243 
00244    drawCanvasDot( x + r2, y, r2, kRed);
00245    y -= r;
00246    latex->DrawLatex( x + 3 * r2, y, "Extra Pixel Hits" );
00247    y -= lineH;
00248 
00249    m_legend->SetY2(y);
00250    m_legend->Draw();
00251    m_legend = 0; // Deleted together with TPad.
00252 }
00253 
00254 void
00255 FWTrackHitsDetailView::makeLegend( void )
00256 {
00257    m_legend = new TLegend( 0.01, 0.01, 0.99, 0.99, 0, "NDC" );
00258    m_legend->SetFillColor(kWhite);
00259    m_legend->SetTextSize( 0.07 );
00260    m_legend->SetBorderSize( 0 );
00261    m_legend->SetMargin( 0.15 );
00262    m_legend->SetEntrySeparation( 0.01 );
00263 
00264    TEveStraightLineSet *legend = new TEveStraightLineSet( "siStripCluster" );
00265    legend->SetLineWidth( 3 );
00266    legend->SetLineColor( kGreen );
00267    m_legend->AddEntry( legend, "Exact SiStripCluster", "l");
00268 
00269    TEveStraightLineSet *legend2 = new TEveStraightLineSet( "siStripCluster2" );
00270    legend2->SetLineWidth( 3 );
00271    legend2->SetLineColor( kRed );
00272    m_legend->AddEntry( legend2, "Extra SiStripCluster", "l");
00273 
00274    TEveStraightLineSet *legend3 = new TEveStraightLineSet( "refStates" );
00275    legend3->SetDepthTest( kFALSE );
00276    legend3->SetMarkerColor( kYellow );
00277    legend3->SetMarkerStyle( kPlus );
00278    legend3->SetMarkerSize( 2 );
00279    m_legend->AddEntry( legend3, "Inner/Outermost States", "p");
00280 
00281    TEveStraightLineSet *legend4 = new TEveStraightLineSet( "vertex" );
00282    legend4->SetDepthTest( kFALSE );
00283    legend4->SetMarkerColor( kRed );
00284    legend4->SetMarkerStyle( kFullDotLarge );
00285    legend4->SetMarkerSize( 2 );
00286    m_legend->AddEntry( legend4, "Vertex", "p");
00287 
00288    TEveStraightLineSet *legend5 = new TEveStraightLineSet( "cameraCenter" );
00289    legend5->SetDepthTest( kFALSE );
00290    legend5->SetMarkerColor( kCyan );
00291    legend5->SetMarkerStyle( kFullDotLarge );
00292    legend5->SetMarkerSize( 2 );
00293    m_legend->AddEntry( legend5, "Camera center", "p");
00294 }
00295 
00296 void
00297 FWTrackHitsDetailView::addTrackerHits3D( std::vector<TVector3> &points, class TEveElementList *tList, Color_t color, int size ) 
00298 {
00299    // !AT this is  detail view specific, should move to track hits
00300    // detail view
00301 
00302    TEvePointSet* pointSet = new TEvePointSet();
00303    pointSet->SetMarkerSize(size);
00304    pointSet->SetMarkerStyle(4);
00305    pointSet->SetPickable(kTRUE);
00306    pointSet->SetTitle("Pixel Hits");
00307    pointSet->SetMarkerColor(color);
00308                 
00309    for( std::vector<TVector3>::const_iterator it = points.begin(), itEnd = points.end(); it != itEnd; ++it) {
00310       pointSet->SetNextPoint(it->x(), it->y(), it->z());
00311    }
00312    tList->AddElement(pointSet);
00313 }
00314 
00315 void
00316 FWTrackHitsDetailView::addHits( const reco::Track& track,
00317                                 const FWEventItem* iItem,
00318                                 TEveElement* trkList,
00319                                 bool addNearbyHits )
00320 {
00321    std::vector<TVector3> pixelPoints;
00322    fireworks::pushPixelHits( pixelPoints, *iItem, track );
00323    TEveElementList* pixels = new TEveElementList( "Pixels" );
00324    trkList->AddElement( pixels );
00325    if( addNearbyHits )
00326    {
00327       // get the extra hits
00328       std::vector<TVector3> pixelExtraPoints;
00329       fireworks::pushNearbyPixelHits( pixelExtraPoints, *iItem, track );
00330       // draw first the others
00331       addTrackerHits3D( pixelExtraPoints, pixels, kRed, 1 );
00332       // then the good ones, so they're on top
00333       addTrackerHits3D( pixelPoints, pixels, kGreen, 1 );
00334    }
00335    else
00336    {
00337       // just add those points with the default color
00338       addTrackerHits3D( pixelPoints, pixels, iItem->defaultDisplayProperties().color(), 1 );
00339    }
00340 
00341    // strips
00342    TEveElementList* strips = new TEveElementList( "Strips" );
00343    trkList->AddElement( strips );
00344    fireworks::addSiStripClusters( iItem, track, strips, addNearbyHits, false );
00345 }
00346 
00347 //______________________________________________________________________________
00348 
00349 void
00350 FWTrackHitsDetailView::addModules( const reco::Track& track,
00351                                    const FWEventItem* iItem,
00352                                    TEveElement* trkList,
00353                                    bool addLostHits )
00354 {
00355    std::set<unsigned int> ids;
00356    for( trackingRecHit_iterator recIt = track.recHitsBegin(), recItEnd = track.recHitsEnd();
00357         recIt != recItEnd; ++recIt )
00358    {
00359       DetId detid = (*recIt)->geographicalId();
00360       if( !addLostHits && !(*recIt)->isValid()) continue;
00361       if( detid.rawId() != 0 )
00362       {
00363          TString name("");
00364          switch( detid.det())
00365          {
00366          case DetId::Tracker:
00367             switch( detid.subdetId())
00368             {
00369             case SiStripDetId::TIB:
00370                name = "TIB ";
00371                break;
00372             case SiStripDetId::TOB:
00373                name = "TOB ";
00374                break;
00375             case SiStripDetId::TID:
00376                name = "TID ";
00377                break;
00378             case SiStripDetId::TEC:
00379                name = "TEC ";
00380                break;
00381             case PixelSubdetector::PixelBarrel:
00382                name = "Pixel Barrel ";
00383                break;
00384             case PixelSubdetector::PixelEndcap:
00385                name = "Pixel Endcap ";
00386             default:
00387                break;
00388             }
00389             break;
00390             
00391          case DetId::Muon:
00392             switch( detid.subdetId())
00393             {
00394             case MuonSubdetId::DT:
00395                name = "DT";
00396                detid = DetId( DTChamberId( detid )); // get rid of layer bits
00397                break;
00398             case MuonSubdetId::CSC:
00399                name = "CSC";
00400                break;
00401             case MuonSubdetId::RPC:
00402                name = "RPC";
00403                break;
00404             default:
00405                break;
00406             }
00407             break;
00408          default:
00409             break;
00410          }
00411          if( ! ids.insert( detid.rawId()).second ) continue;
00412          if( iItem->getGeom())
00413          {
00414             TEveGeoShape* shape = iItem->getGeom()->getEveShape( detid );
00415             if( 0 != shape )
00416             {
00417                shape->SetMainTransparency( 65 );
00418                shape->SetPickable( kTRUE );
00419                switch(( *recIt )->type())
00420                {
00421                case TrackingRecHit::valid:
00422                   shape->SetMainColor( iItem->defaultDisplayProperties().color());
00423                   break;
00424                case TrackingRecHit::missing:
00425                   name += "LOST ";
00426                   shape->SetMainColor( kRed );
00427                   break;
00428                case TrackingRecHit::inactive:
00429                   name += "INACTIVE ";
00430                   shape->SetMainColor( 28 );
00431                   break;
00432                case TrackingRecHit::bad:
00433                   name += "BAD ";
00434                   shape->SetMainColor( 218 );
00435                   break;
00436                }
00437                shape->SetTitle( name + ULong_t( detid.rawId()));
00438                trkList->AddElement( shape );
00439             }
00440             else
00441             {
00442                fwLog( fwlog::kInfo ) <<  "Failed to get shape extract for a tracking rec hit: "
00443                                      << "\n" << fireworks::info( detid ) << std::endl;
00444             }
00445          }
00446       }
00447    }
00448 }
00449 
00450 void
00451 FWTrackHitsDetailView::rnrLabels()
00452 {
00453    m_moduleLabels->SetRnrChildren(!m_moduleLabels->GetRnrChildren());
00454    gEve->Redraw3D();
00455 }
00456 
00457 REGISTER_FWDETAILVIEW(FWTrackHitsDetailView, Hits);