CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Fireworks/Core/src/FWEveView.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWEveView
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  Alja Mrak-Tadel
00010 //         Created:  Thu Mar 16 14:11:32 CET 2010
00011 // $Id: FWEveView.cc,v 1.53 2011/03/25 18:02:46 amraktad Exp $
00012 //
00013 
00014 
00015 
00016 #include <RVersion.h>
00017 #include <boost/bind.hpp>
00018 #include <stdexcept>
00019 
00020 
00021 // user include files
00022 
00023 #define private public  //!!! TODO add get/sets for camera zoom and FOV
00024 #include "TGLOrthoCamera.h"
00025 #include "TGLPerspectiveCamera.h"
00026 #undef private
00027 #include "TGLCameraGuide.h"
00028 
00029 #include "TGLEmbeddedViewer.h"
00030 #include "TEveViewer.h"
00031 #include "TGLScenePad.h"
00032 #include "TEveManager.h"
00033 #include "TEveElement.h"
00034 #include "TEveWindow.h"
00035 #include "TEveScene.h"
00036 #define protected public  //!!! TODO add get/sets for TEveCalo2D for CellIDs
00037 #include "TEveCalo.h"
00038 #undef protected
00039 #include "TGLOverlay.h"
00040 
00041 #include "Fireworks/Core/interface/FWEveView.h"
00042 #include "Fireworks/Core/interface/FWViewType.h"
00043 #include "Fireworks/Core/interface/CmsShowViewPopup.h"
00044 #include "Fireworks/Core/interface/FWEventAnnotation.h"
00045 #include "Fireworks/Core/interface/CmsAnnotation.h"
00046 #include "Fireworks/Core/interface/FWGLEventHandler.h"
00047 #include "Fireworks/Core/interface/FWViewContextMenuHandlerGL.h"
00048 #include "Fireworks/Core/interface/FWConfiguration.h"
00049 #include "Fireworks/Core/interface/FWColorManager.h"
00050 #include "Fireworks/Core/interface/fwLog.h"
00051 #include "Fireworks/Core/interface/Context.h"
00052 #include "Fireworks/Core/interface/FWViewContext.h"
00053 #include "Fireworks/Core/interface/FWViewEnergyScale.h"
00054 #include "Fireworks/Core/interface/CmsShowCommon.h"
00055 #include "Fireworks/Core/interface/FWViewEnergyScaleEditor.h"
00056 
00057 namespace fireworks
00058 {
00059 class Context;
00060 }
00061 
00062 /* This class is temporary workaround for missing in TGLAnnotation functionality */
00063 class ScaleAnnotation : public TGLAnnotation
00064 {
00065 public:
00066    ScaleAnnotation(TGLViewerBase* parent, const char* text, Float_t posx, Float_t posy):
00067       TGLAnnotation(parent, text, posx, posy) {}
00068    virtual ~ScaleAnnotation() {}
00069 
00070    void setText(const char* txt)
00071    {
00072       fText = txt;
00073    }
00074 };
00075 
00076 //
00077 // constructors and destructor
00078 //
00079 
00080 FWEveView::FWEveView(TEveWindowSlot* iParent, FWViewType::EType type, unsigned int version) :
00081    FWViewBase(type, version),
00082    m_viewer(0),
00083    m_eventScene(0),
00084    m_ownedProducts(0),
00085    m_geoScene(0),
00086    m_overlayEventInfo(0),
00087    m_overlayLogo(0),
00088    m_energyMaxValAnnotation(0),
00089    m_cameraGuide(0),
00090    m_context(0),
00091    // style
00092 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,0)
00093    m_imageScale(this, "Image Scale", 1.0, 1.0, 6.0),
00094 #endif
00095    m_eventInfoLevel(this, "Overlay Event Info", 0l, 0l, 2l),
00096    m_drawCMSLogo(this,"Show Logo",false),
00097    m_pointSmooth(this, "Smooth points", false),
00098    m_pointSize(this, "Point size", 1.0, 1.0, 10.0),
00099    m_lineSmooth(this, "Smooth lines", false),
00100    m_lineWidth(this,"Line width",1.0,1.0,10.0),
00101    m_lineOutlineScale(this, "Outline width scale", 1.0, 0.01, 10.0),
00102    m_lineWireframeScale(this, "Wireframe width scale", 1.0, 0.01, 10.0),
00103    m_showCameraGuide(this,"Show Camera Guide",false),
00104    m_useGlobalEnergyScale(this, "UseGlobalEnergyScale", true),
00105    m_viewContext( new FWViewContext()),
00106    m_localEnergyScale( new FWViewEnergyScale(FWViewType::idToName(type), version)),
00107    m_viewEnergyScaleEditor(0)
00108 {
00109    m_viewer = new TEveViewer(typeName().c_str());
00110 
00111    TGLEmbeddedViewer* embeddedViewer;
00112 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,25,4)
00113    embeddedViewer =  m_viewer->SpawnGLEmbeddedViewer(0);
00114 #else
00115    embeddedViewer =  m_viewer->SpawnGLEmbeddedViewer();
00116 #endif
00117    iParent->ReplaceWindow(m_viewer);
00118    gEve->GetViewers()->AddElement(m_viewer);
00119 
00120    m_eventScene =  gEve->SpawnNewScene(Form("EventScene %s", typeName().c_str()));
00121    m_ownedProducts = new TEveElementList("ViewSpecificProducts");
00122    m_eventScene->AddElement(m_ownedProducts);
00123 
00124    m_viewer->AddScene(m_eventScene);
00125 
00126    // spawn geo scene
00127    m_geoScene = gEve->SpawnNewScene(Form("GeoScene %s", typeName().c_str()));
00128    m_geoScene->GetGLScene()->SetSelectable(kFALSE);
00129    m_viewer->AddScene(m_geoScene);
00130 
00131    FWGLEventHandler* eh = new FWGLEventHandler((TGWindow*)embeddedViewer->GetGLWidget(), (TObject*)embeddedViewer);
00132    embeddedViewer->SetEventHandler(eh);
00133    eh->openSelectedModelContextMenu_.connect(openSelectedModelContextMenu_);
00134    eh->SetDoInternalSelection(kFALSE);
00135    FWViewContextMenuHandlerGL* ctxHand = new FWViewContextMenuHandlerGL(this);
00136    // ctxHand->setPickCameraCenter(true);
00137    m_viewContextMenu.reset(ctxHand);
00138    
00139    m_energyMaxValAnnotation = new ScaleAnnotation(viewerGL(), "empty", 0.1, 0.9);
00140    m_energyMaxValAnnotation->SetRole(TGLOverlayElement::kViewer);
00141    m_energyMaxValAnnotation->SetState(TGLOverlayElement::kInvisible);
00142    m_energyMaxValAnnotation->SetUseColorSet(false);
00143    m_energyMaxValAnnotation->SetTextSize(0.05);
00144    m_energyMaxValAnnotation->SetTextColor(kMagenta);
00145 
00146    // style params
00147 
00148    m_overlayEventInfo = new FWEventAnnotation(embeddedViewer);
00149    m_overlayEventInfo->setLevel(0);
00150 
00151    m_eventInfoLevel.addEntry(0, "Nothing");
00152    m_eventInfoLevel.addEntry(1, "Run / event");
00153    m_eventInfoLevel.addEntry(2, "Run / event / lumi");
00154    m_eventInfoLevel.addEntry(3, "Full");
00155    m_eventInfoLevel.changed_.connect(boost::bind(&FWEventAnnotation::setLevel,m_overlayEventInfo, _1));
00156    
00157    m_overlayLogo = new CmsAnnotation(embeddedViewer, 0.02, 0.98);
00158    m_overlayLogo->setVisible(false);
00159    m_drawCMSLogo.changed_.connect(boost::bind(&CmsAnnotation::setVisible,m_overlayLogo, _1));
00160 
00161    m_cameraGuide = new TGLCameraGuide(0.9, 0.1, 0.08);
00162    m_cameraGuide->SetState(TGLOverlayElement::kInvisible);
00163    embeddedViewer->AddOverlayElement(m_cameraGuide);
00164    m_showCameraGuide.changed_.connect(boost::bind(&FWEveView::cameraGuideChanged,this));
00165 
00166    m_pointSmooth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00167    m_pointSize.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00168    m_lineSmooth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00169    m_lineWidth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00170    m_lineOutlineScale.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00171    m_lineWireframeScale.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00172 
00173 
00174    // create scale for view  .. 
00175    m_viewContext->setEnergyScale(m_localEnergyScale.get());
00176    m_useGlobalEnergyScale.changed_.connect(boost::bind(&FWEveView::useGlobalEnergyScaleChanged, this));
00177    m_localEnergyScale->parameterChanged_.connect(boost::bind(&FWEveView::setupEnergyScale, this));
00178 }
00179 
00180 FWEveView::~FWEveView()
00181 {
00182    m_geoScene->RemoveElements();
00183    m_eventScene->RemoveElements();
00184    m_viewer->DestroyWindowAndSlot();
00185 }
00186 
00187 //______________________________________________________________________________
00188 // const member functions
00189 
00190 
00191 FWViewContextMenuHandlerBase* 
00192 FWEveView::contextMenuHandler() const {
00193    return dynamic_cast<FWViewContextMenuHandlerBase*> (m_viewContextMenu.get());
00194 }
00195 
00196 TGLViewer* 
00197 FWEveView::viewerGL() const
00198 {
00199    return  m_viewer->GetGLViewer();
00200 }
00201 
00202 void
00203 FWEveView::saveImageTo(const std::string& iName) const
00204 {
00205    bool succeeded = false;
00206 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,0)
00207    succeeded = viewerGL()->SavePictureScale(iName, m_imageScale.value());
00208 #else
00209    succeeded = viewerGL()->SavePicture(iName.c_str());
00210 #endif
00211 
00212    if(!succeeded) {
00213       throw std::runtime_error("Unable to save picture");
00214    }
00215    fwLog(fwlog::kInfo) <<  "Saved image " << iName << std::endl;
00216 }
00217 
00218 //-------------------------------------------------------------------------------
00219 void
00220 FWEveView::pointLineScalesChanged()
00221 {
00222    viewerGL()->SetSmoothPoints(m_pointSmooth.value());
00223    viewerGL()->SetPointScale  (m_pointSize.value());
00224    viewerGL()->SetSmoothLines (m_lineSmooth.value());
00225    viewerGL()->SetLineScale   (m_lineWidth.value());
00226    viewerGL()->SetOLLineW     (m_lineOutlineScale.value());
00227    viewerGL()->SetWFLineW     (m_lineWireframeScale.value());
00228    viewerGL()->Changed();
00229    gEve->Redraw3D();
00230 }
00231 
00232 void
00233 FWEveView::cameraGuideChanged()
00234 {
00235    m_cameraGuide->SetBinaryState(m_showCameraGuide.value());
00236    viewerGL()->Changed();
00237    gEve->Redraw3D();
00238 }
00239 
00240 void
00241 FWEveView::eventBegin()
00242 {
00243 }
00244 
00245 void
00246 FWEveView::eventEnd()
00247 {
00248    m_overlayEventInfo->setEvent();
00249    setupEnergyScale();
00250 }
00251 
00252 void
00253 FWEveView::setBackgroundColor(Color_t iColor)
00254 {
00255    FWColorManager::setColorSetViewer(viewerGL(), iColor);
00256 }
00257 
00258 void
00259 FWEveView::resetCamera()
00260 {
00261    viewerGL()->ResetCurrentCamera();
00262 }
00263 
00264 //______________________________________________________________________________
00265 void 
00266 FWEveView::setContext(const fireworks::Context& x)
00267 {
00268    m_context = &x ;
00269 
00270    // in constructor view context has local scale
00271    if (m_useGlobalEnergyScale.value()) 
00272       m_viewContext->setEnergyScale(context().commonPrefs()->getEnergyScale());
00273 }
00274 
00275 bool
00276 FWEveView::isEnergyScaleGlobal() const
00277 {
00278    return m_useGlobalEnergyScale.value();
00279 }
00280 
00281 void
00282 FWEveView::useGlobalEnergyScaleChanged()
00283 {
00284    m_viewContext->setEnergyScale(m_useGlobalEnergyScale.value() ? context().commonPrefs()->getEnergyScale() : m_localEnergyScale.get());
00285    if (m_viewEnergyScaleEditor) m_viewEnergyScaleEditor->setEnabled(!m_useGlobalEnergyScale.value());
00286    setupEnergyScale();
00287 }
00288 
00289 void
00290 FWEveView::voteCaloMaxVal()
00291 {
00292    TEveCaloViz* calo = getEveCalo();
00293    if (calo)
00294       context().voteMaxEtAndEnergy(calo->GetData()->GetMaxVal(1), calo->GetData()->GetMaxVal(0));
00295 }
00296 
00297 void
00298 FWEveView::setupEnergyScale()
00299 {
00300    // Called at end of event OR if scale parameters changed.
00301 
00302    FWViewEnergyScale*  energyScale = viewContext()->getEnergyScale();
00303    // printf("setupEnergyScale %s >> scale name %s\n", typeName().c_str(), energyScale->name().c_str());
00304    voteCaloMaxVal();
00305    
00306    // set cache for energy to lenght conversion
00307    float maxVal = context().getMaxEnergyInEvent(energyScale->getPlotEt());
00308    energyScale->updateScaleFactors(maxVal);
00309    // printf("max event val %f \n", maxVal);
00310    // printf("scales lego %f \n",  energyScale->getScaleFactorLego());
00311 
00312    // configure TEveCaloViz
00313    TEveCaloViz* calo = getEveCalo();
00314    if (calo)
00315    {
00316       calo->SetPlotEt(energyScale->getPlotEt());
00317       if (FWViewType::isLego(typeId()))
00318       {
00319          float f = energyScale->getScaleFactorLego();
00320          calo->SetMaxValAbs(TMath::Pi()/f);
00321       }
00322       else
00323       {
00324          float f = energyScale->getScaleFactor3D();
00325          calo->SetMaxValAbs(100/f);
00326       }
00327       calo->ElementChanged();
00328    }
00329 
00330    // emit signal to proxy builders 
00331    viewContext()->scaleChanged();
00332    gEve->Redraw3D();
00333 }
00334 
00335 //-------------------------------------------------------------------------------
00336 void
00337 FWEveView::addTo(FWConfiguration& iTo) const
00338 {
00339    // take care of parameters
00340    FWConfigurableParameterizable::addTo(iTo);
00341    
00342    { 
00343       assert ( m_overlayEventInfo );
00344       m_overlayEventInfo->addTo(iTo);
00345    }
00346    { 
00347       assert ( m_overlayLogo );
00348       m_overlayLogo->addTo(iTo);
00349    }
00350 
00351    m_viewContext->getEnergyScale()->addTo(iTo);
00352 }
00353 
00354 void
00355 FWEveView::setFrom(const FWConfiguration& iFrom)
00356 {
00357    // Make sure you change the version ranges here
00358    // whenever you update the logic.
00359    // The rationale should be:
00360    // (version range supported by the next block) && (version range in the configuration file)
00361    //
00362    // This is not "forward" compatible, but I don't think
00363    // we care.
00364    if (version() >= 2 && iFrom.version() >= 1)
00365    {
00366       for(const_iterator it =begin(), itEnd = end();
00367           it != itEnd;
00368           ++it) {
00369          (*it)->setFrom(iFrom);      
00370       }  
00371    }
00372    if (iFrom.version() > 1)
00373    {
00374       assert( m_overlayEventInfo);
00375       m_overlayEventInfo->setFrom(iFrom);
00376    }
00377    {
00378       assert( m_overlayLogo);
00379       m_overlayLogo->setFrom(iFrom);
00380    }
00381    
00382    if (iFrom.version() > 4)
00383    {
00384       m_localEnergyScale->setFrom(iFrom);
00385    }
00386 }
00387 
00388 //______________________________________________________________________________
00389 
00390 
00391 void
00392 FWEveView::addToOrthoCamera(TGLOrthoCamera* camera, FWConfiguration& iTo) const
00393 {
00394    // zoom
00395    std::ostringstream s;
00396    s<<(camera->fZoom);
00397    std::string name("cameraZoom");
00398    iTo.addKeyValue(name+typeName(),FWConfiguration(s.str()));
00399    
00400    // transformation matrix
00401    std::string matrixName("cameraMatrix");
00402    for ( unsigned int i = 0; i < 16; ++i ) {
00403       std::ostringstream osIndex;
00404       osIndex << i;
00405       std::ostringstream osValue;
00406       osValue << camera->GetCamTrans()[i];
00407       iTo.addKeyValue(matrixName+osIndex.str()+typeName(),FWConfiguration(osValue.str()));
00408    }   
00409 }
00410 
00411 void
00412 FWEveView::setFromOrthoCamera(TGLOrthoCamera* camera,  const FWConfiguration& iFrom)
00413 {
00414    try {
00415       // zoom
00416       std::string zoomName("cameraZoom"); zoomName += typeName();
00417       if (iFrom.valueForKey(zoomName) == 0 )
00418       {
00419          throw std::runtime_error("can't restore parameter cameraZoom");
00420       }
00421       std::istringstream s(iFrom.valueForKey(zoomName)->value());
00422       s>>(camera->fZoom);
00423       
00424       // transformation matrix
00425       std::string matrixName("cameraMatrix");
00426       for ( unsigned int i = 0; i < 16; ++i ) {
00427          std::ostringstream os;
00428          os << i;
00429          const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + typeName() );
00430          if ( value ==  0 )
00431          {
00432             throw std::runtime_error ("can't restore parameter cameraMatrix.");
00433          }
00434          std::istringstream s(value->value());
00435          s>> (camera->RefCamTrans()[i]);
00436       }
00437    }
00438    catch (const std::runtime_error& iException)
00439    {
00440       fwLog(fwlog::kInfo) << "Caught exception while restoring camera parameters in view " << typeName() << "\n.";
00441       viewerGL()->ResetCamerasAfterNextUpdate();      
00442 
00443    }
00444    camera->IncTimeStamp();
00445 }
00446  
00447 void
00448 FWEveView::addToPerspectiveCamera(TGLPerspectiveCamera* cam, const std::string& name, FWConfiguration& iTo) const
00449 {   
00450    // transformation matrix
00451    std::string matrixName("cameraMatrix");
00452    for ( unsigned int i = 0; i < 16; ++i ){
00453       std::ostringstream osIndex;
00454       osIndex << i;
00455       std::ostringstream osValue;
00456       osValue << (cam->GetCamTrans())[i];
00457       iTo.addKeyValue(matrixName+osIndex.str()+name,FWConfiguration(osValue.str()));
00458    }
00459    
00460    // transformation matrix base
00461    matrixName = "cameraMatrixBase";
00462    for ( unsigned int i = 0; i < 16; ++i ){
00463       std::ostringstream osIndex;
00464       osIndex << i;
00465       std::ostringstream osValue;
00466       osValue << (cam->GetCamBase())[i];
00467       iTo.addKeyValue(matrixName+osIndex.str()+name,FWConfiguration(osValue.str()));
00468    }
00469    {
00470       std::ostringstream osValue;
00471       osValue << cam->fFOV;
00472       iTo.addKeyValue(name+" FOV",FWConfiguration(osValue.str()));
00473    }
00474 }
00475 
00476 void
00477 FWEveView::setFromPerspectiveCamera(TGLPerspectiveCamera* cam, const std::string& name, const FWConfiguration& iFrom)
00478 {
00479    try {
00480       std::string matrixName("cameraMatrix");
00481       for ( unsigned int i = 0; i < 16; ++i ){
00482          std::ostringstream os;
00483          os << i;
00484          const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + name );
00485          if ( value ==  0 )
00486          {
00487             throw std::runtime_error ("can't restore parameter cameraMatrix.");
00488          }
00489          std::istringstream s(value->value());
00490          s>>((cam->RefCamTrans())[i]);
00491       }
00492       
00493       // transformation matrix base
00494       matrixName = "cameraMatrixBase";
00495       for ( unsigned int i = 0; i < 16; ++i ){
00496          std::ostringstream os;
00497          os << i;
00498          const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + name );
00499          if ( value ==  0 )
00500          {
00501             throw std::runtime_error ("can't restore parameter cameraMatrixBase.");
00502          }
00503      
00504          std::istringstream s(value->value());
00505          s>>((cam->RefCamBase())[i]);
00506       }
00507       
00508       {
00509          const FWConfiguration* value = iFrom.valueForKey( name + " FOV" );
00510          if ( value ==  0 )
00511          {
00512             throw std::runtime_error ("can't restore parameter cameraMatrixBase.");
00513          }
00514          std::istringstream s(value->value());
00515          s>>cam->fFOV;
00516       }
00517       
00518       cam->IncTimeStamp();
00519    }
00520    catch (const std::runtime_error& iException)
00521    {
00522       fwLog(fwlog::kInfo) << "Caught exception while restoring camera parameters in view " << typeName() << "\n.";
00523       viewerGL()->ResetCamerasAfterNextUpdate();  
00524       fwLog(fwlog::kDebug) << "Reset camera fo view "  << typeName() << "\n.";
00525    }
00526 }
00527 
00528 
00529 void 
00530 FWEveView::populateController(ViewerParameterGUI& gui) const
00531 {
00532    gui.requestTab("Style").
00533       addParam(&m_eventInfoLevel).
00534       addParam(&m_drawCMSLogo).
00535       addParam(&m_showCameraGuide).
00536       separator().
00537 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,0)
00538       addParam(&m_imageScale).
00539 #endif
00540       addParam(&m_pointSize).
00541       addParam(&m_pointSmooth).
00542       addParam(&m_lineSmooth).
00543       addParam(&m_lineWidth).
00544       addParam(&m_lineOutlineScale).
00545       addParam(&m_lineWireframeScale);
00546 
00547 
00548    gui.requestTab("Scales").
00549       addParam(&m_useGlobalEnergyScale);
00550 
00551    m_viewEnergyScaleEditor = new FWViewEnergyScaleEditor(m_localEnergyScale.get(), gui.getTabContainer(), !FWViewType::isLego(typeId()));
00552    m_viewEnergyScaleEditor->setEnabled(!m_useGlobalEnergyScale.value());
00553    gui.addFrameToContainer(m_viewEnergyScaleEditor);
00554 }