CMS 3D CMS Logo

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