CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/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.44 2011/03/26 10:15:10 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 FWRPZViewGeometry* FWRPZView::s_geometryList = 0;
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_calo(0),
00054    m_shiftOrigin(this,"Shift origin to beam-spot", true),
00055    m_fishEyeDistortion(this,"Distortion",0., 0., 100.),
00056    m_fishEyeR(this,"FixedRadius",(double)fireworks::Context::caloR1(), 0.0, 150.0),
00057 
00058    m_caloDistortion(this,"Calo compression",1.0,0.01,10.),
00059    m_muonDistortion(this,"Muon compression",0.2,0.01,10.),
00060    m_showProjectionAxes(this,"Show projection axis", false),
00061    m_projectionAxesLabelSize(this,"Projection axis label size", 0.015, 0.001, 0.2),
00062    m_compressMuon(this,"Compress detectors",false),
00063    m_showHF(0),
00064    m_showEndcaps(0)
00065 {
00066 
00067    TEveProjection::EPType_e projType = (id == FWViewType::kRhoZ) ? TEveProjection::kPT_RhoZ : TEveProjection::kPT_RPhi;
00068 
00069    m_projMgr = new TEveProjectionManager(projType);
00070    m_projMgr->IncDenyDestroy();
00071    m_projMgr->SetImportEmpty(kTRUE);
00072 
00073    m_projMgr->GetProjection()->SetDistortion(m_fishEyeDistortion.value()*s_distortF);
00074    m_projMgr->GetProjection()->SetFixR(m_fishEyeR.value());
00075          
00076 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00077    m_projMgr->GetProjection()->SetDisplaceOrigin( m_shiftOrigin.value());
00078 #endif
00079 
00080    if ( id == FWViewType::kRhoPhi || id == FWViewType::kRhoPhiPF) {
00081       m_projMgr->GetProjection()->AddPreScaleEntry(0, fireworks::Context::caloR1(), 1.0);
00082       m_projMgr->GetProjection()->AddPreScaleEntry(0, 300, 0.2);
00083    } else {
00084       m_projMgr->GetProjection()->AddPreScaleEntry(0, fireworks::Context::caloR1(), 1.0);
00085       m_projMgr->GetProjection()->AddPreScaleEntry(1, 310, 1.0);
00086       m_projMgr->GetProjection()->AddPreScaleEntry(0, 370, 0.2);
00087       m_projMgr->GetProjection()->AddPreScaleEntry(1, 580, 0.2);
00088    }
00089  
00090 
00091    // camera  
00092    viewerGL()->SetCurrentCamera(TGLViewer::kCameraOrthoXOY);
00093    if ( TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>( &(viewerGL()->CurrentCamera()) ) ) {
00094       camera->SetZoomMax(1e6);
00095    }
00096    geoScene()->GetGLScene()->SetSelectable(kFALSE);
00097 
00098    m_axes = new TEveProjectionAxes(m_projMgr);
00099    m_axes->SetRnrState(m_showProjectionAxes.value());
00100    m_axes->SetLabelSize(m_projectionAxesLabelSize.value());
00101    m_showProjectionAxes.changed_.connect(boost::bind(&FWRPZView::showProjectionAxes,this));
00102    m_projectionAxesLabelSize.changed_.connect(boost::bind(&FWRPZView::projectionAxesLabelSize,this));
00103    eventScene()->AddElement(m_axes);
00104 
00105    if ( id != FWViewType::kRhoZ ) {
00106       m_showEndcaps = new FWBoolParameter(this,"Include EndCaps", true);
00107       m_showEndcaps->changed_.connect(  boost::bind(&FWRPZView::setEtaRng, this) );
00108       m_showHF = new FWBoolParameter(this,"Include HF", true);
00109       m_showHF->changed_.connect(  boost::bind(&FWRPZView::setEtaRng, this) );
00110    }
00111 
00112    m_shiftOrigin.changed_.connect(boost::bind(&FWRPZView::doShiftOriginToBeamSpot,this));
00113 
00114    m_fishEyeDistortion.changed_.connect(boost::bind(&FWRPZView::doFishEyeDistortion,this));
00115    m_fishEyeR.changed_.connect(boost::bind(&FWRPZView::doFishEyeDistortion,this));
00116 
00117    m_caloDistortion.changed_.connect(boost::bind(&FWRPZView::doPreScaleDistortion,this));
00118    m_muonDistortion.changed_.connect(boost::bind(&FWRPZView::doPreScaleDistortion,this));
00119    m_compressMuon.changed_.connect(boost::bind(&FWRPZView::doCompression,this,_1));
00120 }
00121 
00122 FWRPZView::~FWRPZView()
00123 {
00124    m_calo->Destroy();
00125    m_projMgr->DecDenyDestroy();
00126 }
00127 
00128 //
00129 // member functions
00130 //
00131 
00132 TEveCaloViz*
00133 FWRPZView::getEveCalo() const
00134 {
00135    return static_cast<TEveCaloViz*>(m_calo);
00136 }
00137 
00138 void
00139 FWRPZView::setContext(const fireworks::Context& ctx)
00140 {
00141    FWEveView::setContext(ctx);
00142 
00143    if (!s_geometryList)
00144    {
00145       s_geometryList = new  FWRPZViewGeometry(ctx);
00146       s_geometryList->IncDenyDestroy();
00147    }
00148    m_projMgr->ImportElements(s_geometryList->getGeoElements(typeId()), geoScene());
00149 
00150    TEveCaloData* data = context().getCaloData();
00151 
00152    TEveCalo3D* calo3d = new TEveCalo3D(data);
00153 
00154    m_calo = static_cast<TEveCalo2D*> (m_projMgr->ImportElements(calo3d, eventScene()));
00155 
00156    if (typeId() == FWViewType::kRhoPhiPF)
00157       m_calo->SetBarrelRadius(177);
00158    else
00159       m_calo->SetBarrelRadius(context().caloR1(false));
00160 
00161    m_calo->SetEndCapPos(context().caloZ1(false));
00162    m_calo->SetAutoRange(false);
00163    m_calo->SetScaleAbs(true);
00164 }
00165 
00166 void
00167 FWRPZView::eventBegin()
00168 {  
00169    if (context().getBeamSpot())
00170    {
00171       FWBeamSpot& b = *(context().getBeamSpot());
00172       fwLog(fwlog::kDebug) << Form("%s::eventBegin Set projection center (%f, %f, %f) \n", typeName().c_str(), b.x0(), b.y0(), b.z0());
00173 
00174       // projection center
00175       TEveVector center(b.x0(),  b.y0(), b.z0());
00176       m_projMgr->GetProjection()->SetCenter(center);
00177 
00178       // camera move
00179       TGLCamera& cam = viewerGL()->CurrentCamera();
00180       cam.SetExternalCenter(true);
00181       if (typeId() != FWViewType::kRhoZ)
00182       {
00183          double r = TMath::Sqrt( b.x0()*b.x0() +  b.y0()*b.y0());
00184          cam.SetCenterVec(b.z0(), TMath::Sign(r, b.y0()), 0);
00185       }
00186       else
00187       {
00188          cam.SetCenterVec(b.x0(), b.y0(), b.z0());
00189       }
00190    }
00191 }
00192 
00193 void
00194 FWRPZView::doShiftOriginToBeamSpot()
00195 { 
00196 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00197 
00198    TEveProjection* p = m_projMgr->GetProjection();
00199    if (p->GetDisplaceOrigin() != m_shiftOrigin.value())
00200    {
00201       p->SetDisplaceOrigin( m_shiftOrigin.value());
00202       m_projMgr->ProjectChildren();
00203       gEve->Redraw3D();
00204    }
00205 #endif
00206 }
00207 
00208 void
00209 FWRPZView::shiftOrigin(TEveVector& center)
00210 { 
00211 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00212    // re-project with new center
00213    m_projMgr->GetProjection()->SetCenter(center);
00214    m_projMgr->ProjectChildren();
00215 
00216    // draw projected center
00217    float* pc =  m_projMgr->GetProjection()->GetProjectedCenter();
00218    viewerGL()->CurrentCamera().SetExternalCenter(true);
00219    viewerGL()->CurrentCamera().SetCenterVec(pc[0], pc[1], pc[2]);
00220    viewerGL()->SetDrawCameraCenter(true);
00221 
00222    gEve->Redraw3D();
00223 #endif
00224 }
00225 
00226 void
00227 FWRPZView::resetOrigin()
00228 { 
00229    // set center back to beam spot
00230 
00231 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00232    FWBeamSpot& b = *(context().getBeamSpot());
00233    TEveVector center(b.x0(),  b.y0(), b.z0());
00234    m_projMgr->GetProjection()->SetCenter(center);
00235 
00236    m_projMgr->ProjectChildren();
00237    gEve->Redraw3D();
00238 #endif
00239 }
00240 
00241 
00242 void
00243 FWRPZView::doFishEyeDistortion()
00244 {
00245    TEveProjection* p = m_projMgr->GetProjection();
00246     if (p->GetDistortion() != m_fishEyeDistortion.value()*s_distortFInv)
00247    p->SetDistortion(m_fishEyeDistortion.value()*s_distortF);
00248    if (p->GetFixR() != m_fishEyeR.value())
00249       p->SetFixR(m_fishEyeR.value());
00250 
00251    m_projMgr->ProjectChildren();
00252    gEve->Redraw3D();
00253 }
00254 
00255 void
00256 FWRPZView::doPreScaleDistortion()
00257 {
00258    if ( typeId() == FWViewType::kRhoPhi || typeId() == FWViewType::kRhoPhiPF ) {
00259       m_projMgr->GetProjection()->ChangePreScaleEntry(0,1,m_caloDistortion.value());
00260       m_projMgr->GetProjection()->ChangePreScaleEntry(0,2,m_muonDistortion.value());
00261    } else {
00262       m_projMgr->GetProjection()->ChangePreScaleEntry(0,1,m_caloDistortion.value());
00263       m_projMgr->GetProjection()->ChangePreScaleEntry(0,2,m_muonDistortion.value());
00264       m_projMgr->GetProjection()->ChangePreScaleEntry(1,1,m_caloDistortion.value());
00265       m_projMgr->GetProjection()->ChangePreScaleEntry(1,2,m_muonDistortion.value());
00266    }
00267    m_projMgr->UpdateName();
00268    m_projMgr->ProjectChildren();
00269    gEve->Redraw3D();
00270 }
00271 
00272 void
00273 FWRPZView::doCompression(bool flag)
00274 {
00275    m_projMgr->GetProjection()->SetUsePreScale(flag);
00276    m_projMgr->UpdateName();
00277    m_projMgr->ProjectChildren();
00278    gEve->Redraw3D();
00279 }
00280 
00281 void
00282 FWRPZView::importElements(TEveElement* iChildren, float iLayer, TEveElement* iProjectedParent)
00283 {
00284    float oldLayer = m_projMgr->GetCurrentDepth();
00285    m_projMgr->SetCurrentDepth(iLayer);
00286    //make sure current depth is reset even if an exception is thrown
00287    boost::shared_ptr<TEveProjectionManager> sentry(m_projMgr,
00288                                                    boost::bind(&TEveProjectionManager::SetCurrentDepth,
00289                                                                _1,oldLayer));
00290    m_projMgr->ImportElements(iChildren,iProjectedParent);
00291 }
00292 
00293 
00294 void
00295 FWRPZView::addTo(FWConfiguration& iTo) const
00296 {
00297    FWEveView::addTo(iTo);
00298    TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>( &(viewerGL()->CurrentCamera()) );
00299    if (camera) addToOrthoCamera(camera, iTo);
00300 }
00301 
00302 void
00303 FWRPZView::setFrom(const FWConfiguration& iFrom)
00304 {
00305    FWEveView::setFrom(iFrom);
00306    
00307    TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>( &(viewerGL()->CurrentCamera()) );
00308    if (camera) setFromOrthoCamera(camera, iFrom);
00309 
00310    if (iFrom.version() < 7)
00311    { 
00312       const FWConfiguration* value = iFrom.valueForKey("Show projection axes");
00313       if (value)
00314          m_showProjectionAxes.set(value->value() == "1");
00315    }
00316 }
00317 
00318 void
00319 FWRPZView::setEtaRng()
00320 {
00321    if (typeId() != FWViewType::kRhoZ)
00322    {
00323       // rng controllers only in RhoPhi
00324       double eta_range = context().caloMaxEta();
00325       if (!m_showHF->value() ) eta_range = 3.0;
00326       if (!m_showEndcaps->value() ) eta_range = context().caloTransEta();
00327       m_calo->SetEta(-eta_range,eta_range);
00328    }
00329 
00330    FWEveView::setupEnergyScale();
00331 }
00332 
00333 void
00334 FWRPZView::voteCaloMaxVal()
00335 {
00336    if (! m_calo->GetData()->Empty())
00337    {
00338       m_calo->AssertCellIdCache();
00339       Float_t sumEt, sumE;
00340       TEveCaloData::CellData_t cellData;
00341       typedef std::vector<TEveCaloData::vCellId_t*>           vBinCells_t;
00342       typedef std::vector<TEveCaloData::vCellId_t*>::iterator vBinCells_i;
00343 
00344       vBinCells_t   cellLists = m_calo->fCellLists;
00345       for (vBinCells_i it = cellLists.begin(); it != cellLists.end(); it++)
00346       {
00347          TEveCaloData::vCellId_t* binCells = *it;
00348          if (binCells) {
00349             sumEt =  0; sumE = 0;
00350             TEveCaloData::vCellId_i a = binCells->end();
00351       
00352             for (TEveCaloData::vCellId_i k = binCells->begin(); k != a; ++k)
00353             { 
00354                m_calo->GetData()->GetCellData((*k), cellData);
00355                sumEt += cellData.Value(true);
00356                sumE  += cellData.Value(false);
00357             } 
00358             // printf("vote sum %f %f \n", sumEt , sumE);
00359             context().voteMaxEtAndEnergy(sumEt, sumE);
00360          }
00361       }
00362    }
00363 }
00364 
00365 void FWRPZView::showProjectionAxes( )
00366 {   
00367    m_axes->SetRnrState(m_showProjectionAxes.value());
00368    gEve->Redraw3D();
00369    viewerGL()->RequestDraw();
00370 }
00371 
00372 void FWRPZView::projectionAxesLabelSize( )
00373 {   
00374    m_axes->SetLabelSize(m_projectionAxesLabelSize.value());
00375    viewerGL()->RequestDraw();
00376 }
00377 
00378 void 
00379 FWRPZView::populateController(ViewerParameterGUI& gui) const
00380 {
00381    FWEveView::populateController(gui);   
00382 
00383 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
00384    gui.requestTab("Projection").addParam(&m_shiftOrigin);
00385 #endif
00386 
00387    gui.requestTab("Projection").
00388    addParam(&m_showProjectionAxes).
00389    addParam(&m_projectionAxesLabelSize).
00390    separator();
00391 
00392    TGCompositeFrame* f = gui.getTabContainer();
00393 
00394    f->AddFrame(new TGLabel(f, "FishEye:"));
00395    gui.addParam(&m_fishEyeDistortion).
00396       addParam(&m_fishEyeR).
00397       separator();
00398 
00399    f->AddFrame(new TGLabel(f, "PreScales:"));
00400 
00401    gui.requestTab("Projection").
00402       addParam(&m_compressMuon).
00403       addParam(&m_muonDistortion).
00404       addParam(&m_caloDistortion);
00405 
00406 
00407    if (typeId() == FWViewType::kRhoPhi || typeId() == FWViewType::kRhoPhiPF) 
00408    {
00409       gui.requestTab("Calo").
00410          addParam(m_showHF).
00411          addParam(m_showEndcaps);
00412    }
00413 }
00414