CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/Fireworks/Core/src/FWLegoViewBase.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWLegoViewBase
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Thu Feb 21 11:22:41 EST 2008
00011 // $Id: FWLegoViewBase.cc,v 1.29 2011/02/22 18:37:31 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <boost/bind.hpp>
00016 
00017 #include "TAxis.h"
00018 #include "TH2.h"
00019 
00020 #include "TGLViewer.h"
00021 #include "TGLLightSet.h"
00022 #include "TGLPerspectiveCamera.h"
00023 #include "TGLOrthoCamera.h"
00024 #include "TEveManager.h"
00025 #include "TEveElement.h"
00026 #include "TEveScene.h"
00027 #include "TEveCalo.h"
00028 #include "TEveTrans.h"
00029 #include "TEveScene.h"
00030 #include "TEveCaloLegoOverlay.h"
00031 
00032 // user include files
00033 #include "Fireworks/Core/interface/FWGLEventHandler.h"
00034 #include "Fireworks/Core/interface/FWConfiguration.h"
00035 #include "Fireworks/Core/interface/FWLegoViewBase.h"
00036 #include "Fireworks/Core/interface/FWViewEnergyScale.h"
00037 #include "Fireworks/Core/interface/FWViewContext.h"
00038 #include "Fireworks/Core/interface/CmsShowViewPopup.h"
00039 #include "Fireworks/Core/interface/fw3dlego_xbins.h"
00040 
00041 
00042 //
00043 // constants, enums and typedefs
00044 //
00045 
00046 //
00047 // static data member definitions
00048 //
00049 
00050 //
00051 // constructors and destructor
00052 //
00053 FWLegoViewBase::FWLegoViewBase(TEveWindowSlot* iParent, FWViewType::EType typeId) :
00054    FWEveView(iParent, typeId),
00055    m_lego(0),
00056    m_overlay(0),
00057    m_autoRebin(this,"Auto rebin on zoom-out",false),
00058    m_pixelsPerBin(this, "Pixels per bin", 10., 1., 20.),
00059    m_projectionMode(this, "Projection", 0l, 0l, 2l),
00060    m_cell2DMode(this, "Cell2DMode", 1l, 1l, 2l),
00061    m_drawValuesIn2D(this,"Draw Cell2D threshold (pixels)",40l,16l,200l),
00062    m_showOverlay(this,"Draw scales", true)
00063 {
00064    viewerGL()->SetCurrentCamera(TGLViewer::kCameraOrthoXOY);
00065    viewerGL()->GetLightSet()->SetUseSpecular(false);
00066 
00067    m_projectionMode.addEntry(0, "Auto");
00068    m_projectionMode.addEntry(1, "3D");
00069    m_projectionMode.addEntry(2, "2D");
00070 
00071    m_cell2DMode.addEntry(1, "Plain");
00072    m_cell2DMode.addEntry(2, "Outline");
00073    if (typeId == FWViewType::kLegoHF) m_cell2DMode.set(2); // different default for HF view
00074 
00075    m_autoRebin.changed_.connect(boost::bind(&FWLegoViewBase::setAutoRebin,this));
00076    m_pixelsPerBin.changed_.connect(boost::bind(&FWLegoViewBase::setPixelsPerBin,this));
00077    m_drawValuesIn2D.changed_.connect(boost::bind(&FWLegoViewBase::setFontSizein2D,this));
00078    m_showOverlay.changed_.connect(boost::bind(&FWLegoViewBase::showOverlay,this));
00079    m_projectionMode.changed_.connect(boost::bind(&FWLegoViewBase::setProjectionMode, this));
00080    m_cell2DMode.changed_.connect(boost::bind(&FWLegoViewBase::setCell2DMode, this));
00081 }
00082 
00083 FWLegoViewBase::~FWLegoViewBase()
00084 {
00085    viewerGL()->RemoveOverlayElement(m_overlay);
00086    m_lego->Destroy();
00087 }
00088 
00089 
00090 TEveCaloViz*
00091 FWLegoViewBase::getEveCalo() const
00092 {
00093    return static_cast<TEveCaloViz*>(m_lego);
00094 }
00095 
00096 void 
00097 FWLegoViewBase::setContext(const fireworks::Context& ctx)
00098 {
00099    FWEveView::setContext(ctx);
00100   
00101    TEveCaloData* data;
00102    if (typeId() == FWViewType::kLego)  {
00103       data = static_cast<TEveCaloData*>(ctx.getCaloData());
00104    }
00105    else if (typeId() == FWViewType::kLegoHF) {
00106       data = static_cast<TEveCaloData*>(ctx.getCaloDataHF());
00107    }
00108    else
00109    {
00110       // create empty data locally instead of context
00111       TEveCaloDataHist* hdata = new TEveCaloDataHist();
00112       hdata->IncDenyDestroy();
00113 
00114       Bool_t status = TH1::AddDirectoryStatus();
00115       TH1::AddDirectory(kFALSE); //Keeps histogram from going into memory
00116       TH2F* dummy = new TH2F("background",
00117                              "background",
00118                              fw3dlego::xbins_n - 1, fw3dlego::xbins,
00119                              72, -1*TMath::Pi(), TMath::Pi());
00120       
00121       TH1::AddDirectory(status);
00122       Int_t sliceIndex = hdata->AddHistogram(dummy);
00123       (hdata)->RefSliceInfo(sliceIndex).Setup("background", 0., 0);
00124       data = hdata;
00125    } 
00126 
00127    data->GetEtaBins()->SetNdivisions(710);
00128    data->GetPhiBins()->SetNdivisions(510);
00129    data->GetEtaBins()->SetTitleFont(120);
00130    data->GetEtaBins()->SetTitle("h");
00131    data->GetPhiBins()->SetTitleFont(120);
00132    data->GetPhiBins()->SetTitle("f");
00133    data->GetEtaBins()->SetLabelSize(0.02);
00134    data->GetEtaBins()->SetTitleSize(0.03);
00135 
00136    m_lego = new TEveCaloLego(data);
00137    m_lego->Set2DMode((TEveCaloLego::E2DMode_e)m_cell2DMode.value());
00138    m_lego->SetDrawNumberCellPixels(m_drawValuesIn2D.value());
00139    m_lego->SetAutoRebin(m_autoRebin.value());
00140    m_lego->SetPixelsPerBin(m_pixelsPerBin.value());
00141 
00142    m_lego->InitMainTrans();
00143    m_lego->RefMainTrans().SetScale(TMath::TwoPi(), TMath::TwoPi(), TMath::Pi());
00144    m_lego->SetScaleAbs(true);
00145    
00146    // set flat in 2D
00147    m_lego->SetHasFixedHeightIn2DMode(true);
00148    m_lego->SetFixedHeightValIn2DMode(0.001);
00149    eventScene()->AddElement(m_lego);
00150 
00151    TEveLegoEventHandler* eh = dynamic_cast<TEveLegoEventHandler*>( viewerGL()->GetEventHandler());
00152    if (eh) eh->SetLego(m_lego);
00153   
00154    m_overlay = new TEveCaloLegoOverlay();
00155    m_overlay->SetCaloLego(m_lego);
00156    m_overlay->SetShowPlane(kFALSE);
00157    m_overlay->SetScalePosition(0.8, 0.6);
00158    m_overlay->SetShowScales(1); //temporary
00159    viewerGL()->AddOverlayElement(m_overlay);
00160 }
00161    
00162 void
00163 FWLegoViewBase::setAutoRebin()
00164 {
00165    m_lego->SetAutoRebin(m_autoRebin.value());
00166    m_lego->ElementChanged(kTRUE,kTRUE);
00167 }
00168 
00169 void
00170 FWLegoViewBase::setPixelsPerBin()
00171 {
00172    m_lego->SetPixelsPerBin((Int_t) (m_pixelsPerBin.value()));
00173    m_lego->ElementChanged(kTRUE,kTRUE);
00174 }
00175 
00176 void
00177 FWLegoViewBase::showOverlay()
00178 {
00179    if (m_overlay) m_overlay->SetShowScales(m_showOverlay.value());
00180    viewerGL()->Changed();
00181    gEve->Redraw3D();
00182 }
00183 
00184 //_______________________________________________________________________________
00185 
00186 void
00187 FWLegoViewBase::setFrom(const FWConfiguration& iFrom)
00188 {
00189    FWEveView::setFrom(iFrom);
00190 
00191    // cell 2D style
00192    if (iFrom.version() < 5)
00193    {
00194       const FWConfiguration* value = iFrom.valueForKey( "Cell2DMode" );
00195       if ( value !=  0 )
00196       {
00197          int mode;
00198          std::istringstream s(value->value());
00199          s>> mode;
00200          m_cell2DMode.set(mode);
00201       }
00202   
00203    }
00204 
00205    // view controller parameters, changed name in version 4
00206    if (iFrom.version() < 4)
00207    {
00208       bool xb;/* double xd;
00209       {
00210          std::istringstream s(iFrom.valueForKey("Lego auto scale")->value());
00211          s >> xb; m_energyScaleMode.set(xb ? FWEveView::kAutoScale : FWEveView::kFixedScale);
00212       }
00213       {
00214          std::istringstream s(iFrom.valueForKey("Lego scale GeV)")->value());
00215          s >> xd; m_energyMaxAbsVal.set(xd);
00216          }*/
00217       {
00218          std::istringstream s(iFrom.valueForKey("Show scales")->value());
00219          s >> xb; m_showOverlay.set(xb);
00220       }
00221       {
00222          std::istringstream s(iFrom.valueForKey("Show scales")->value());
00223          s >> xb; m_showOverlay.set(xb);
00224       }
00225       {
00226          std::istringstream s(iFrom.valueForKey("Auto rebin on zoom")->value());
00227          s >> xb; m_autoRebin.set(xb);
00228       }
00229    }
00230 
00231    //
00232    // camera restore
00233 
00234    if (iFrom.version() > 1)
00235    {
00236       bool topView = true;
00237       std::string stateName("topView"); stateName += typeName();
00238       assert( 0 != iFrom.valueForKey(stateName));
00239       std::istringstream s(iFrom.valueForKey(stateName)->value());
00240       s >> topView;
00241 
00242 
00243       if (topView)
00244       {
00245          viewerGL()->SetCurrentCamera(TGLViewer::kCameraOrthoXOY);
00246          TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>( &(viewerGL()->RefCamera(TGLViewer::kCameraOrthoXOY)) );
00247          setFromOrthoCamera(camera, iFrom);
00248       }
00249       else
00250       {
00251          viewerGL()->SetCurrentCamera(TGLViewer::kCameraPerspXOY);
00252          TGLPerspectiveCamera* camera = dynamic_cast<TGLPerspectiveCamera*>(&(viewerGL()->RefCamera(TGLViewer::kCameraPerspXOY)));
00253          setFromPerspectiveCamera(camera, typeName(), iFrom);
00254       }
00255    }
00256    else
00257    {
00258       // reset camera if version not supported    
00259       viewerGL()->ResetCamerasAfterNextUpdate();
00260    }
00261    
00262 }
00263 
00264 void
00265 FWLegoViewBase::addTo(FWConfiguration& iTo) const
00266 {
00267    FWEveView::addTo(iTo);
00268    
00269    bool topView =  viewerGL()->CurrentCamera().IsOrthographic();
00270    std::ostringstream s;
00271    s << topView;
00272    std::string name = "topView";
00273    iTo.addKeyValue(name+typeName(),FWConfiguration(s.str()));
00274    
00275    if (topView)
00276    {
00277       TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>(&(viewerGL()->RefCamera(TGLViewer::kCameraOrthoXOY)));
00278       addToOrthoCamera(camera, iTo);  
00279    }
00280    else
00281    {
00282       TGLPerspectiveCamera* camera = dynamic_cast<TGLPerspectiveCamera*>(&(viewerGL()->RefCamera(TGLViewer::kCameraPerspXOY)));
00283       addToPerspectiveCamera(camera, typeName(), iTo);   
00284    }
00285 }
00286 
00287 void
00288 FWLegoViewBase::setFontSizein2D()
00289 {
00290    m_lego->SetDrawNumberCellPixels( m_drawValuesIn2D.value());
00291    m_lego->ElementChanged(kTRUE,kTRUE);
00292 }
00293 
00294 void
00295 FWLegoViewBase::setProjectionMode()
00296 {
00297    m_lego->SetProjection((TEveCaloLego::EProjection_e)m_projectionMode.value());
00298    m_lego->ElementChanged();
00299    viewerGL()->Changed();
00300    gEve->Redraw3D();
00301 }
00302 
00303 void
00304 FWLegoViewBase::setCell2DMode()
00305 {
00306    m_lego->Set2DMode((TEveCaloLego::E2DMode_e)m_cell2DMode.value());
00307    m_lego->ElementChanged();
00308    viewerGL()->Changed();
00309    gEve->Redraw3D();
00310 }
00311 
00312 void 
00313 FWLegoViewBase::populateController(ViewerParameterGUI& gui) const
00314 {
00315    FWEveView::populateController(gui);
00316 
00317    gui.requestTab("Style").
00318       separator().
00319       addParam(&m_projectionMode).
00320       addParam(&m_cell2DMode).
00321       addParam(&m_drawValuesIn2D);
00322   
00323    gui.requestTab("Scales").
00324       separator().
00325       addParam(&m_showOverlay);
00326 
00327    gui.requestTab("Rebin").
00328       addParam(&m_autoRebin).
00329       addParam(&m_pixelsPerBin);
00330 }