CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/Fireworks/Core/src/FWRPZView.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWRPZView
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Tue Feb 19 10:33:25 EST 2008
00011 // $Id: FWRPZView.cc,v 1.46 2011/10/15 02:33:30 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <stdexcept>
00016 #include <boost/bind.hpp>
00017 #include <boost/shared_ptr.hpp>
00018 
00019 #include "TGLViewer.h"
00020 #include "TGLScenePad.h"
00021 #include "TEveManager.h"
00022 #include "TEveElement.h"
00023 #include "TEveScene.h"
00024 #include "TEveProjections.h"
00025 #include "TEveProjectionAxes.h"
00026 #include "TGLabel.h"
00027 #include "TEveProjectionManager.h"
00028 
00030 #define protected public  
00031 #include "TEveCalo.h"
00032 #undef protected
00033 
00034 // user include files
00035 #include "Fireworks/Core/interface/FWRPZView.h"
00036 #include "Fireworks/Core/interface/FWRPZViewGeometry.h"
00037 #include "Fireworks/Core/interface/FWBeamSpot.h"
00038 #include "Fireworks/Core/interface/Context.h"
00039 #include "Fireworks/Core/interface/fwLog.h"
00040 #include "Fireworks/Core/interface/FWViewContext.h"
00041 #include "Fireworks/Core/interface/FWViewContext.h"
00042 #include "Fireworks/Core/interface/FWViewEnergyScale.h"
00043 #include "Fireworks/Core/interface/CmsShowViewPopup.h"
00044 
00045 
00046 const float FWRPZView::s_distortF = 0.001;
00047 const float FWRPZView::s_distortFInv = 1000;
00048 //
00049 // constructors and destructor
00050 //
00051 FWRPZView::FWRPZView(TEveWindowSlot* iParent, FWViewType::EType id) :
00052    FWEveView(iParent, id, 7),
00053    m_geometryList(0),
00054    m_projMgr(0),
00055    m_axes(0),
00056 
00057    m_calo(0),
00058 
00059    m_showPixelBarrel(this, "Show Pixel Barrel", false ),
00060    m_showPixelEndcap(this, "Show Pixel Endcap", false),
00061    m_showTrackerBarrel(this, "Show Tracker Barrel", false ),
00062    m_showTrackerEndcap(this, "Show Tracker Endcap", false),
00063 
00064    m_shiftOrigin(this,"Shift origin to beam-spot", false),
00065    m_fishEyeDistortion(this,"Distortion",0., 0., 100.),
00066    m_fishEyeR(this,"FixedRadius",(double)fireworks::Context::caloR1(), 0.0, 150.0),
00067 
00068    m_caloDistortion(this,"Calo compression",1.0,0.01,10.),
00069    m_muonDistortion(this,"Muon compression",0.2,0.01,10.),
00070    m_showProjectionAxes(this,"Show projection axis", false),
00071    m_projectionAxesLabelSize(this,"Projection axis label size", 0.015, 0.001, 0.2),
00072    m_compressMuon(this,"Compress detectors",false),
00073    m_showHF(0),
00074    m_showEndcaps(0)
00075 {
00076    TEveProjection::EPType_e projType = (id == FWViewType::kRhoZ) ? TEveProjection::kPT_RhoZ : TEveProjection::kPT_RPhi;
00077 
00078    m_projMgr = new TEveProjectionManager(projType);
00079    m_projMgr->IncDenyDestroy();
00080    m_projMgr->SetImportEmpty(kTRUE);
00081 
00082    m_projMgr->GetProjection()->SetDistortion(m_fishEyeDistortion.value()*s_distortF);
00083    m_projMgr->GetProjection()->SetFixR(m_fishEyeR.value());
00084          
00085 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00086    m_projMgr->GetProjection()->SetDisplaceOrigin( m_shiftOrigin.value());
00087 #endif
00088 
00089    if ( id == FWViewType::kRhoPhi || id == FWViewType::kRhoPhiPF) {
00090       m_projMgr->GetProjection()->AddPreScaleEntry(0, fireworks::Context::caloR1(), 1.0);
00091       m_projMgr->GetProjection()->AddPreScaleEntry(0, 300, 0.2);
00092    } else {
00093       m_projMgr->GetProjection()->AddPreScaleEntry(0, fireworks::Context::caloR1(), 1.0);
00094       m_projMgr->GetProjection()->AddPreScaleEntry(1, 310, 1.0);
00095       m_projMgr->GetProjection()->AddPreScaleEntry(0, 370, 0.2);
00096       m_projMgr->GetProjection()->AddPreScaleEntry(1, 580, 0.2);
00097    }
00098  
00099 
00100    // camera  
00101    viewerGL()->SetCurrentCamera(TGLViewer::kCameraOrthoXOY);
00102    if ( TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>( &(viewerGL()->CurrentCamera()) ) ) {
00103       camera->SetZoomMax(1e6);
00104    }
00105    geoScene()->GetGLScene()->SetSelectable(kFALSE);
00106 
00107    m_axes = new TEveProjectionAxes(m_projMgr);
00108    m_axes->SetRnrState(m_showProjectionAxes.value());
00109    m_axes->SetLabelSize(m_projectionAxesLabelSize.value());
00110    m_showProjectionAxes.changed_.connect(boost::bind(&FWRPZView::showProjectionAxes,this));
00111    m_projectionAxesLabelSize.changed_.connect(boost::bind(&FWRPZView::projectionAxesLabelSize,this));
00112    eventScene()->AddElement(m_axes);
00113 
00114    if ( id != FWViewType::kRhoZ ) {
00115       m_showEndcaps = new FWBoolParameter(this,"Include EndCaps", true);
00116       m_showEndcaps->changed_.connect(  boost::bind(&FWRPZView::setEtaRng, this) );
00117       m_showHF = new FWBoolParameter(this,"Include HF", true);
00118       m_showHF->changed_.connect(  boost::bind(&FWRPZView::setEtaRng, this) );
00119    }
00120   
00121    m_shiftOrigin.changed_.connect(boost::bind(&FWRPZView::doShiftOriginToBeamSpot,this));
00122 
00123    m_fishEyeDistortion.changed_.connect(boost::bind(&FWRPZView::doFishEyeDistortion,this));
00124 
00125    m_fishEyeR.changed_.connect(boost::bind(&FWRPZView::doFishEyeDistortion,this));
00126 
00127    m_caloDistortion.changed_.connect(boost::bind(&FWRPZView::doPreScaleDistortion,this));
00128    m_muonDistortion.changed_.connect(boost::bind(&FWRPZView::doPreScaleDistortion,this));
00129    m_compressMuon.changed_.connect(boost::bind(&FWRPZView::doCompression,this,_1));
00130 }
00131 
00132 FWRPZView::~FWRPZView()
00133 {
00134    m_calo->Destroy();
00135    m_projMgr->DecDenyDestroy();
00136 }
00137 
00138 //
00139 // member functions
00140 //
00141 
00142 TEveCaloViz*
00143 FWRPZView::getEveCalo() const
00144 {
00145    return static_cast<TEveCaloViz*>(m_calo);
00146 }
00147 
00148 void
00149 FWRPZView::setContext(const fireworks::Context& ctx)
00150 {
00151    FWEveView::setContext(ctx);
00152 
00153    m_geometryList = new  FWRPZViewGeometry(ctx);
00154    m_geometryList->IncDenyDestroy();
00155    m_geometryList->initStdGeoElements(typeId());
00156    TEveElement* p = m_projMgr->ImportElements(m_geometryList);
00157    geoScene()->AddElement(p);
00158 
00159    TEveCaloData* data = context().getCaloData();
00160 
00161    TEveCalo3D* calo3d = new TEveCalo3D(data);
00162 
00163    m_calo = static_cast<TEveCalo2D*> (m_projMgr->ImportElements(calo3d, eventScene()));
00164 
00165    if (typeId() == FWViewType::kRhoPhiPF)
00166       m_calo->SetBarrelRadius(177);
00167    else
00168       m_calo->SetBarrelRadius(context().caloR1(false));
00169 
00170    m_calo->SetEndCapPos(context().caloZ1(false));
00171    m_calo->SetAutoRange(false);
00172    m_calo->SetScaleAbs(true);
00173 
00174    m_showPixelBarrel.changed_.connect(boost::bind(&FWRPZViewGeometry::showPixelBarrel,m_geometryList,_1));
00175    m_showPixelEndcap.changed_.connect(boost::bind(&FWRPZViewGeometry::showPixelEndcap,m_geometryList,_1));
00176    m_showTrackerBarrel.changed_.connect(boost::bind(&FWRPZViewGeometry::showTrackerBarrel,m_geometryList,_1));
00177    m_showTrackerEndcap.changed_.connect(boost::bind(&FWRPZViewGeometry::showTrackerEndcap,m_geometryList,_1));
00178 
00179 }
00180 
00181 void
00182 FWRPZView::eventBegin()
00183 {  
00184    if (context().getBeamSpot())
00185    {
00186       FWBeamSpot& b = *(context().getBeamSpot());
00187       fwLog(fwlog::kDebug) << Form("%s::eventBegin Set projection center (%f, %f, %f) \n", typeName().c_str(), b.x0(), b.y0(), b.z0());
00188 
00189       // projection center
00190       TEveVector center(b.x0(),  b.y0(), b.z0());
00191       m_projMgr->GetProjection()->SetCenter(center);
00192 
00193       // camera move
00194       TGLCamera& cam = viewerGL()->CurrentCamera();
00195       cam.SetExternalCenter(true);
00196       if (typeId() != FWViewType::kRhoZ)
00197       {
00198          double r = TMath::Sqrt( b.x0()*b.x0() +  b.y0()*b.y0());
00199          cam.SetCenterVec(b.z0(), TMath::Sign(r, b.y0()), 0);
00200       }
00201       else
00202       {
00203          cam.SetCenterVec(b.x0(), b.y0(), b.z0());
00204       }
00205    }
00206 }
00207 
00208 void
00209 FWRPZView::eventEnd()
00210 {
00211    FWEveView::eventEnd();
00212    viewerGL()->RequestDraw();
00213 }
00214 
00215 void
00216 FWRPZView::doShiftOriginToBeamSpot()
00217 { 
00218 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00219 
00220    TEveProjection* p = m_projMgr->GetProjection();
00221    if (p->GetDisplaceOrigin() != m_shiftOrigin.value())
00222    {
00223       p->SetDisplaceOrigin( m_shiftOrigin.value());
00224       m_projMgr->ProjectChildren();
00225       gEve->Redraw3D();
00226    }
00227 #endif
00228 }
00229 
00230 void
00231 FWRPZView::shiftOrigin(TEveVector& center)
00232 { 
00233 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00234    // re-project with new center
00235    m_projMgr->GetProjection()->SetCenter(center);
00236    m_projMgr->ProjectChildren();
00237 
00238    // draw projected center
00239    float* pc =  m_projMgr->GetProjection()->GetProjectedCenter();
00240    viewerGL()->CurrentCamera().SetExternalCenter(true);
00241    viewerGL()->CurrentCamera().SetCenterVec(pc[0], pc[1], pc[2]);
00242    viewerGL()->SetDrawCameraCenter(true);
00243 
00244    gEve->Redraw3D();
00245 #endif
00246 }
00247 
00248 void
00249 FWRPZView::resetOrigin()
00250 { 
00251    // set center back to beam spot
00252 
00253 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00254    FWBeamSpot& b = *(context().getBeamSpot());
00255    TEveVector center(b.x0(),  b.y0(), b.z0());
00256    m_projMgr->GetProjection()->SetCenter(center);
00257 
00258    m_projMgr->ProjectChildren();
00259    gEve->Redraw3D();
00260 #endif
00261 }
00262 
00263 
00264 void
00265 FWRPZView::doFishEyeDistortion()
00266 {
00267    TEveProjection* p = m_projMgr->GetProjection();
00268     if (p->GetDistortion() != m_fishEyeDistortion.value()*s_distortFInv)
00269    p->SetDistortion(m_fishEyeDistortion.value()*s_distortF);
00270    if (p->GetFixR() != m_fishEyeR.value())
00271       p->SetFixR(m_fishEyeR.value());
00272 
00273    m_projMgr->ProjectChildren();
00274    gEve->Redraw3D();
00275 }
00276 
00277 void
00278 FWRPZView::doPreScaleDistortion()
00279 {
00280    if ( typeId() == FWViewType::kRhoPhi || typeId() == FWViewType::kRhoPhiPF ) {
00281       m_projMgr->GetProjection()->ChangePreScaleEntry(0,1,m_caloDistortion.value());
00282       m_projMgr->GetProjection()->ChangePreScaleEntry(0,2,m_muonDistortion.value());
00283    } else {
00284       m_projMgr->GetProjection()->ChangePreScaleEntry(0,1,m_caloDistortion.value());
00285       m_projMgr->GetProjection()->ChangePreScaleEntry(0,2,m_muonDistortion.value());
00286       m_projMgr->GetProjection()->ChangePreScaleEntry(1,1,m_caloDistortion.value());
00287       m_projMgr->GetProjection()->ChangePreScaleEntry(1,2,m_muonDistortion.value());
00288    }
00289    m_projMgr->UpdateName();
00290    m_projMgr->ProjectChildren();
00291    gEve->Redraw3D();
00292 }
00293 
00294 void
00295 FWRPZView::doCompression(bool flag)
00296 {
00297    m_projMgr->GetProjection()->SetUsePreScale(flag);
00298    m_projMgr->UpdateName();
00299    m_projMgr->ProjectChildren();
00300    gEve->Redraw3D();
00301 }
00302 
00303 void
00304 FWRPZView::importElements(TEveElement* iChildren, float iLayer, TEveElement* iProjectedParent)
00305 {
00306    float oldLayer = m_projMgr->GetCurrentDepth();
00307    m_projMgr->SetCurrentDepth(iLayer);
00308    //make sure current depth is reset even if an exception is thrown
00309    boost::shared_ptr<TEveProjectionManager> sentry(m_projMgr,
00310                                                    boost::bind(&TEveProjectionManager::SetCurrentDepth,
00311                                                                _1,oldLayer));
00312    m_projMgr->ImportElements(iChildren,iProjectedParent);
00313 }
00314 
00315 
00316 void
00317 FWRPZView::addTo(FWConfiguration& iTo) const
00318 {
00319    FWEveView::addTo(iTo);
00320    TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>( &(viewerGL()->CurrentCamera()) );
00321    if (camera) addToOrthoCamera(camera, iTo);
00322 }
00323 
00324 void
00325 FWRPZView::setFrom(const FWConfiguration& iFrom)
00326 {
00327    FWEveView::setFrom(iFrom);
00328    
00329    TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>( &(viewerGL()->CurrentCamera()) );
00330    if (camera) setFromOrthoCamera(camera, iFrom);
00331 
00332    if (iFrom.version() < 7)
00333    { 
00334       const FWConfiguration* value = iFrom.valueForKey("Show projection axes");
00335       if (value)
00336          m_showProjectionAxes.set(value->value() == "1");
00337    }
00338 }
00339 
00340 void
00341 FWRPZView::setEtaRng()
00342 {
00343    if (typeId() != FWViewType::kRhoZ)
00344    {
00345       // rng controllers only in RhoPhi
00346       double eta_range = context().caloMaxEta();
00347       if (!m_showHF->value() ) eta_range = 3.0;
00348       if (!m_showEndcaps->value() ) eta_range = context().caloTransEta();
00349       m_calo->SetEta(-eta_range,eta_range);
00350    }
00351 
00352    FWEveView::setupEnergyScale();
00353 }
00354 
00355 void
00356 FWRPZView::voteCaloMaxVal()
00357 {
00358    if (! m_calo->GetData()->Empty())
00359    {
00360       m_calo->AssertCellIdCache();
00361       Float_t sumEt, sumE;
00362       TEveCaloData::CellData_t cellData;
00363       typedef std::vector<TEveCaloData::vCellId_t*>           vBinCells_t;
00364       typedef std::vector<TEveCaloData::vCellId_t*>::iterator vBinCells_i;
00365 
00366       vBinCells_t   cellLists = m_calo->fCellLists;
00367       for (vBinCells_i it = cellLists.begin(); it != cellLists.end(); it++)
00368       {
00369          TEveCaloData::vCellId_t* binCells = *it;
00370          if (binCells) {
00371             sumEt =  0; sumE = 0;
00372             TEveCaloData::vCellId_i a = binCells->end();
00373       
00374             for (TEveCaloData::vCellId_i k = binCells->begin(); k != a; ++k)
00375             { 
00376                m_calo->GetData()->GetCellData((*k), cellData);
00377                sumEt += cellData.Value(true);
00378                sumE  += cellData.Value(false);
00379             } 
00380             // printf("vote sum %f %f \n", sumEt , sumE);
00381             context().voteMaxEtAndEnergy(sumEt, sumE);
00382          }
00383       }
00384    }
00385 }
00386 
00387 void FWRPZView::showProjectionAxes( )
00388 {   
00389    m_axes->SetRnrState(m_showProjectionAxes.value());
00390    gEve->Redraw3D();
00391    viewerGL()->RequestDraw();
00392 }
00393 
00394 void FWRPZView::projectionAxesLabelSize( )
00395 {   
00396    m_axes->SetLabelSize(m_projectionAxesLabelSize.value());
00397    viewerGL()->RequestDraw();
00398 }
00399 
00400 void 
00401 FWRPZView::populateController(ViewerParameterGUI& gui) const
00402 {
00403    FWEveView::populateController(gui);   
00404 
00405    ViewerParameterGUI& det =  gui.requestTab("Detector");;
00406    det.addParam(&m_showPixelBarrel);
00407    if (typeId() == FWViewType::kRhoZ) det.addParam(&m_showPixelEndcap);
00408    det.addParam(&m_showTrackerBarrel);
00409    if (typeId() == FWViewType::kRhoZ) det.addParam(&m_showTrackerEndcap);
00410 
00411 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00412    gui.requestTab("Projection").addParam(&m_shiftOrigin);
00413 #endif
00414 
00415    gui.requestTab("Projection").
00416    addParam(&m_showProjectionAxes).
00417    addParam(&m_projectionAxesLabelSize).
00418    separator();
00419 
00420    TGCompositeFrame* f = gui.getTabContainer();
00421 
00422    f->AddFrame(new TGLabel(f, "FishEye:"));
00423    gui.addParam(&m_fishEyeDistortion).
00424       addParam(&m_fishEyeR).
00425       separator();
00426 
00427    f->AddFrame(new TGLabel(f, "PreScales:"));
00428 
00429    gui.requestTab("Projection").
00430       addParam(&m_compressMuon).
00431       addParam(&m_muonDistortion).
00432       addParam(&m_caloDistortion);
00433 
00434 
00435    if (typeId() == FWViewType::kRhoPhi || typeId() == FWViewType::kRhoPhiPF) 
00436    {
00437       gui.requestTab("Calo").
00438          addParam(m_showHF).
00439          addParam(m_showEndcaps);
00440    }
00441 }
00442