CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/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.52 2011/02/03 17:38:40 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    m_viewContextMenu.reset(new FWViewContextMenuHandlerGL(m_viewer));
00131 
00132    FWGLEventHandler* eh = new FWGLEventHandler((TGWindow*)embeddedViewer->GetGLWidget(), (TObject*)embeddedViewer);
00133    embeddedViewer->SetEventHandler(eh);
00134    eh->openSelectedModelContextMenu_.connect(openSelectedModelContextMenu_);
00135    eh->SetDoInternalSelection(kFALSE);
00136    FWViewContextMenuHandlerGL* ctxHand = new FWViewContextMenuHandlerGL(m_viewer);
00137    ctxHand->setPickCameraCenter(true);
00138    m_viewContextMenu.reset(ctxHand);
00139    
00140    m_energyMaxValAnnotation = new ScaleAnnotation(viewerGL(), "empty", 0.1, 0.9);
00141    m_energyMaxValAnnotation->SetRole(TGLOverlayElement::kViewer);
00142    m_energyMaxValAnnotation->SetState(TGLOverlayElement::kInvisible);
00143    m_energyMaxValAnnotation->SetUseColorSet(false);
00144    m_energyMaxValAnnotation->SetTextSize(0.05);
00145    m_energyMaxValAnnotation->SetTextColor(kMagenta);
00146 
00147    // style params
00148 
00149    m_overlayEventInfo = new FWEventAnnotation(embeddedViewer);
00150    m_overlayEventInfo->setLevel(0);
00151 
00152    m_eventInfoLevel.addEntry(0, "Nothing");
00153    m_eventInfoLevel.addEntry(1, "Run / event");
00154    m_eventInfoLevel.addEntry(2, "Run / event / lumi");
00155    m_eventInfoLevel.addEntry(3, "Full");
00156    m_eventInfoLevel.changed_.connect(boost::bind(&FWEventAnnotation::setLevel,m_overlayEventInfo, _1));
00157    
00158    m_overlayLogo = new CmsAnnotation(embeddedViewer, 0.02, 0.98);
00159    m_overlayLogo->setVisible(false);
00160    m_drawCMSLogo.changed_.connect(boost::bind(&CmsAnnotation::setVisible,m_overlayLogo, _1));
00161 
00162    m_cameraGuide = new TGLCameraGuide(0.9, 0.1, 0.08);
00163    m_cameraGuide->SetState(TGLOverlayElement::kInvisible);
00164    embeddedViewer->AddOverlayElement(m_cameraGuide);
00165    m_showCameraGuide.changed_.connect(boost::bind(&FWEveView::cameraGuideChanged,this));
00166 
00167    m_pointSmooth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00168    m_pointSize.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00169    m_lineSmooth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00170    m_lineWidth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00171    m_lineOutlineScale.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00172    m_lineWireframeScale.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
00173 
00174 
00175    // create scale for view  .. 
00176    m_viewContext->setEnergyScale(m_localEnergyScale.get());
00177    m_useGlobalEnergyScale.changed_.connect(boost::bind(&FWEveView::useGlobalEnergyScaleChanged, this));
00178    m_localEnergyScale->parameterChanged_.connect(boost::bind(&FWEveView::setupEnergyScale, this));
00179 }
00180 
00181 FWEveView::~FWEveView()
00182 {
00183    m_geoScene->RemoveElements();
00184    m_eventScene->RemoveElements();
00185    m_viewer->DestroyWindowAndSlot();
00186 }
00187 
00188 //______________________________________________________________________________
00189 // const member functions
00190 
00191 
00192 FWViewContextMenuHandlerBase* 
00193 FWEveView::contextMenuHandler() const {
00194    return (FWViewContextMenuHandlerBase*)m_viewContextMenu.get();
00195 }
00196 
00197 TGLViewer* 
00198 FWEveView::viewerGL() const
00199 {
00200    return  m_viewer->GetGLViewer();
00201 }
00202 
00203 void
00204 FWEveView::saveImageTo(const std::string& iName) const
00205 {
00206    bool succeeded = false;
00207 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,0)
00208    succeeded = viewerGL()->SavePictureScale(iName, m_imageScale.value());
00209 #else
00210    succeeded = viewerGL()->SavePicture(iName.c_str());
00211 #endif
00212 
00213    if(!succeeded) {
00214       throw std::runtime_error("Unable to save picture");
00215    }
00216    fwLog(fwlog::kInfo) <<  "Saved image " << iName << std::endl;
00217 }
00218 
00219 //-------------------------------------------------------------------------------
00220 void
00221 FWEveView::pointLineScalesChanged()
00222 {
00223    viewerGL()->SetSmoothPoints(m_pointSmooth.value());
00224    viewerGL()->SetPointScale  (m_pointSize.value());
00225    viewerGL()->SetSmoothLines (m_lineSmooth.value());
00226    viewerGL()->SetLineScale   (m_lineWidth.value());
00227    viewerGL()->SetOLLineW     (m_lineOutlineScale.value());
00228    viewerGL()->SetWFLineW     (m_lineWireframeScale.value());
00229    viewerGL()->Changed();
00230    gEve->Redraw3D();
00231 }
00232 
00233 void
00234 FWEveView::cameraGuideChanged()
00235 {
00236    m_cameraGuide->SetBinaryState(m_showCameraGuide.value());
00237    viewerGL()->Changed();
00238    gEve->Redraw3D();
00239 }
00240 
00241 void
00242 FWEveView::eventBegin()
00243 {
00244 }
00245 
00246 void
00247 FWEveView::eventEnd()
00248 {
00249    m_overlayEventInfo->setEvent();
00250    setupEnergyScale();
00251 }
00252 
00253 void
00254 FWEveView::setBackgroundColor(Color_t iColor)
00255 {
00256    FWColorManager::setColorSetViewer(viewerGL(), iColor);
00257 }
00258 
00259 void
00260 FWEveView::resetCamera()
00261 {
00262    viewerGL()->ResetCurrentCamera();
00263 }
00264 
00265 //______________________________________________________________________________
00266 void 
00267 FWEveView::setContext(const fireworks::Context& x)
00268 {
00269    m_context = &x ;
00270 
00271    // in constructor view context has local scale
00272    if (m_useGlobalEnergyScale.value()) 
00273       m_viewContext->setEnergyScale(context().commonPrefs()->getEnergyScale());
00274 }
00275 
00276 bool
00277 FWEveView::isEnergyScaleGlobal() const
00278 {
00279    return m_useGlobalEnergyScale.value();
00280 }
00281 
00282 void
00283 FWEveView::useGlobalEnergyScaleChanged()
00284 {
00285    m_viewContext->setEnergyScale(m_useGlobalEnergyScale.value() ? context().commonPrefs()->getEnergyScale() : m_localEnergyScale.get());
00286    if (m_viewEnergyScaleEditor) m_viewEnergyScaleEditor->setEnabled(!m_useGlobalEnergyScale.value());
00287    setupEnergyScale();
00288 }
00289 
00290 void
00291 FWEveView::voteCaloMaxVal()
00292 {
00293    TEveCaloViz* calo = getEveCalo();
00294    if (calo)
00295       context().voteMaxEtAndEnergy(calo->GetData()->GetMaxVal(1), calo->GetData()->GetMaxVal(0));
00296 }
00297 
00298 void
00299 FWEveView::setupEnergyScale()
00300 {
00301    // Called at end of event OR if scale parameters changed.
00302 
00303    FWViewEnergyScale*  energyScale = viewContext()->getEnergyScale();
00304    // printf("setupEnergyScale %s >> scale name %s\n", typeName().c_str(), energyScale->name().c_str());
00305    voteCaloMaxVal();
00306    
00307    // set cache for energy to lenght conversion
00308    float maxVal = context().getMaxEnergyInEvent(energyScale->getPlotEt());
00309    energyScale->updateScaleFactors(maxVal);
00310    // printf("max event val %f \n", maxVal);
00311    // printf("scales lego %f \n",  energyScale->getScaleFactorLego());
00312 
00313    // configure TEveCaloViz
00314    TEveCaloViz* calo = getEveCalo();
00315    if (calo)
00316    {
00317       calo->SetPlotEt(energyScale->getPlotEt());
00318       if (FWViewType::isLego(typeId()))
00319       {
00320          float f = energyScale->getScaleFactorLego();
00321          calo->SetMaxValAbs(TMath::Pi()/f);
00322       }
00323       else
00324       {
00325          float f = energyScale->getScaleFactor3D();
00326          calo->SetMaxValAbs(100/f);
00327       }
00328       calo->ElementChanged();
00329    }
00330 
00331    // emit signal to proxy builders 
00332    viewContext()->scaleChanged();
00333    gEve->Redraw3D();
00334 }
00335 
00336 //-------------------------------------------------------------------------------
00337 void
00338 FWEveView::addTo(FWConfiguration& iTo) const
00339 {
00340    // take care of parameters
00341    FWConfigurableParameterizable::addTo(iTo);
00342    
00343    { 
00344       assert ( m_overlayEventInfo );
00345       m_overlayEventInfo->addTo(iTo);
00346    }
00347    { 
00348       assert ( m_overlayLogo );
00349       m_overlayLogo->addTo(iTo);
00350    }
00351 
00352    m_viewContext->getEnergyScale()->addTo(iTo);
00353 }
00354 
00355 void
00356 FWEveView::setFrom(const FWConfiguration& iFrom)
00357 {
00358    // Make sure you change the version ranges here
00359    // whenever you update the logic.
00360    // The rationale should be:
00361    // (version range supported by the next block) && (version range in the configuration file)
00362    //
00363    // This is not "forward" compatible, but I don't think
00364    // we care.
00365    if (version() >= 2 && iFrom.version() >= 1)
00366    {
00367       for(const_iterator it =begin(), itEnd = end();
00368           it != itEnd;
00369           ++it) {
00370          (*it)->setFrom(iFrom);      
00371       }  
00372    }
00373    if (iFrom.version() > 1)
00374    {
00375       assert( m_overlayEventInfo);
00376       m_overlayEventInfo->setFrom(iFrom);
00377    }
00378    {
00379       assert( m_overlayLogo);
00380       m_overlayLogo->setFrom(iFrom);
00381    }
00382    
00383    if (iFrom.version() > 4)
00384    {
00385       m_localEnergyScale->setFrom(iFrom);
00386    }
00387 }
00388 
00389 //______________________________________________________________________________
00390 
00391 
00392 void
00393 FWEveView::addToOrthoCamera(TGLOrthoCamera* camera, FWConfiguration& iTo) const
00394 {
00395    // zoom
00396    std::ostringstream s;
00397    s<<(camera->fZoom);
00398    std::string name("cameraZoom");
00399    iTo.addKeyValue(name+typeName(),FWConfiguration(s.str()));
00400    
00401    // transformation matrix
00402    std::string matrixName("cameraMatrix");
00403    for ( unsigned int i = 0; i < 16; ++i ) {
00404       std::ostringstream osIndex;
00405       osIndex << i;
00406       std::ostringstream osValue;
00407       osValue << camera->GetCamTrans()[i];
00408       iTo.addKeyValue(matrixName+osIndex.str()+typeName(),FWConfiguration(osValue.str()));
00409    }   
00410 }
00411 
00412 void
00413 FWEveView::setFromOrthoCamera(TGLOrthoCamera* camera,  const FWConfiguration& iFrom)
00414 {
00415    try {
00416       // zoom
00417       std::string zoomName("cameraZoom"); zoomName += typeName();
00418       if (iFrom.valueForKey(zoomName) == 0 )
00419       {
00420          throw std::runtime_error("can't restore parameter cameraZoom");
00421       }
00422       std::istringstream s(iFrom.valueForKey(zoomName)->value());
00423       s>>(camera->fZoom);
00424       
00425       // transformation matrix
00426       std::string matrixName("cameraMatrix");
00427       for ( unsigned int i = 0; i < 16; ++i ) {
00428          std::ostringstream os;
00429          os << i;
00430          const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + typeName() );
00431          if ( value ==  0 )
00432          {
00433             throw std::runtime_error ("can't restore parameter cameraMatrix.");
00434          }
00435          std::istringstream s(value->value());
00436          s>> (camera->RefCamTrans()[i]);
00437       }
00438    }
00439    catch (const std::runtime_error& iException)
00440    {
00441       fwLog(fwlog::kInfo) << "Caught exception while restoring camera parameters in view " << typeName() << "\n.";
00442       viewerGL()->ResetCamerasAfterNextUpdate();      
00443 
00444    }
00445    camera->IncTimeStamp();
00446 }
00447  
00448 void
00449 FWEveView::addToPerspectiveCamera(TGLPerspectiveCamera* cam, const std::string& name, FWConfiguration& iTo) const
00450 {   
00451    // transformation matrix
00452    std::string matrixName("cameraMatrix");
00453    for ( unsigned int i = 0; i < 16; ++i ){
00454       std::ostringstream osIndex;
00455       osIndex << i;
00456       std::ostringstream osValue;
00457       osValue << (cam->GetCamTrans())[i];
00458       iTo.addKeyValue(matrixName+osIndex.str()+name,FWConfiguration(osValue.str()));
00459    }
00460    
00461    // transformation matrix base
00462    matrixName = "cameraMatrixBase";
00463    for ( unsigned int i = 0; i < 16; ++i ){
00464       std::ostringstream osIndex;
00465       osIndex << i;
00466       std::ostringstream osValue;
00467       osValue << (cam->GetCamBase())[i];
00468       iTo.addKeyValue(matrixName+osIndex.str()+name,FWConfiguration(osValue.str()));
00469    }
00470    {
00471       std::ostringstream osValue;
00472       osValue << cam->fFOV;
00473       iTo.addKeyValue(name+" FOV",FWConfiguration(osValue.str()));
00474    }
00475 }
00476 
00477 void
00478 FWEveView::setFromPerspectiveCamera(TGLPerspectiveCamera* cam, const std::string& name, const FWConfiguration& iFrom)
00479 {
00480    try {
00481       std::string matrixName("cameraMatrix");
00482       for ( unsigned int i = 0; i < 16; ++i ){
00483          std::ostringstream os;
00484          os << i;
00485          const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + name );
00486          if ( value ==  0 )
00487          {
00488             throw std::runtime_error ("can't restore parameter cameraMatrix.");
00489          }
00490          std::istringstream s(value->value());
00491          s>>((cam->RefCamTrans())[i]);
00492       }
00493       
00494       // transformation matrix base
00495       matrixName = "cameraMatrixBase";
00496       for ( unsigned int i = 0; i < 16; ++i ){
00497          std::ostringstream os;
00498          os << i;
00499          const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + name );
00500          if ( value ==  0 )
00501          {
00502             throw std::runtime_error ("can't restore parameter cameraMatrixBase.");
00503          }
00504      
00505          std::istringstream s(value->value());
00506          s>>((cam->RefCamBase())[i]);
00507       }
00508       
00509       {
00510          const FWConfiguration* value = iFrom.valueForKey( name + " FOV" );
00511          if ( value ==  0 )
00512          {
00513             throw std::runtime_error ("can't restore parameter cameraMatrixBase.");
00514          }
00515          std::istringstream s(value->value());
00516          s>>cam->fFOV;
00517       }
00518       
00519       cam->IncTimeStamp();
00520    }
00521    catch (const std::runtime_error& iException)
00522    {
00523       fwLog(fwlog::kInfo) << "Caught exception while restoring camera parameters in view " << typeName() << "\n.";
00524       viewerGL()->ResetCamerasAfterNextUpdate();  
00525       fwLog(fwlog::kDebug) << "Reset camera fo view "  << typeName() << "\n.";
00526    }
00527 }
00528 
00529 
00530 void 
00531 FWEveView::populateController(ViewerParameterGUI& gui) const
00532 {
00533    gui.requestTab("Style").
00534       addParam(&m_eventInfoLevel).
00535       addParam(&m_drawCMSLogo).
00536       addParam(&m_showCameraGuide).
00537       separator().
00538 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,0)
00539       addParam(&m_imageScale).
00540 #endif
00541       addParam(&m_pointSize).
00542       addParam(&m_pointSmooth).
00543       addParam(&m_lineSmooth).
00544       addParam(&m_lineWidth).
00545       addParam(&m_lineOutlineScale).
00546       addParam(&m_lineWireframeScale);
00547 
00548 
00549    gui.requestTab("Scales").
00550       addParam(&m_useGlobalEnergyScale);
00551 
00552    m_viewEnergyScaleEditor = new FWViewEnergyScaleEditor(m_localEnergyScale.get(), gui.getTabContainer(), !FWViewType::isLego(typeId()));
00553    m_viewEnergyScaleEditor->setEnabled(!m_useGlobalEnergyScale.value());
00554    gui.addFrameToContainer(m_viewEnergyScaleEditor);
00555 }