CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWEveView.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWEveView
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Alja Mrak-Tadel
10 // Created: Thu Mar 16 14:11:32 CET 2010
11 // $Id: FWEveView.cc,v 1.52 2011/02/03 17:38:40 amraktad Exp $
12 //
13 
14 
15 
16 #include <RVersion.h>
17 #include <boost/bind.hpp>
18 #include <stdexcept>
19 
20 
21 // user include files
22 
23 #define private public
24 #include "TGLOrthoCamera.h"
25 #include "TGLPerspectiveCamera.h"
26 #undef private
27 #include "TGLCameraGuide.h"
28 
29 #include "TGLEmbeddedViewer.h"
30 #include "TEveViewer.h"
31 #include "TGLScenePad.h"
32 #include "TEveManager.h"
33 #include "TEveElement.h"
34 #include "TEveWindow.h"
35 #include "TEveScene.h"
36 #define protected public
37 #include "TEveCalo.h"
38 #undef protected
39 #include "TGLOverlay.h"
40 
56 
57 namespace fireworks
58 {
59 class Context;
60 }
61 
62 /* This class is temporary workaround for missing in TGLAnnotation functionality */
63 class ScaleAnnotation : public TGLAnnotation
64 {
65 public:
66  ScaleAnnotation(TGLViewerBase* parent, const char* text, Float_t posx, Float_t posy):
67  TGLAnnotation(parent, text, posx, posy) {}
68  virtual ~ScaleAnnotation() {}
69 
70  void setText(const char* txt)
71  {
72  fText = txt;
73  }
74 };
75 
76 //
77 // constructors and destructor
78 //
79 
80 FWEveView::FWEveView(TEveWindowSlot* iParent, FWViewType::EType type, unsigned int version) :
81  FWViewBase(type, version),
82  m_viewer(0),
83  m_eventScene(0),
84  m_ownedProducts(0),
85  m_geoScene(0),
86  m_overlayEventInfo(0),
87  m_overlayLogo(0),
88  m_energyMaxValAnnotation(0),
89  m_cameraGuide(0),
90  m_context(0),
91  // style
92 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,0)
93  m_imageScale(this, "Image Scale", 1.0, 1.0, 6.0),
94 #endif
95  m_eventInfoLevel(this, "Overlay Event Info", 0l, 0l, 2l),
96  m_drawCMSLogo(this,"Show Logo",false),
97  m_pointSmooth(this, "Smooth points", false),
98  m_pointSize(this, "Point size", 1.0, 1.0, 10.0),
99  m_lineSmooth(this, "Smooth lines", false),
100  m_lineWidth(this,"Line width",1.0,1.0,10.0),
101  m_lineOutlineScale(this, "Outline width scale", 1.0, 0.01, 10.0),
102  m_lineWireframeScale(this, "Wireframe width scale", 1.0, 0.01, 10.0),
103  m_showCameraGuide(this,"Show Camera Guide",false),
104  m_useGlobalEnergyScale(this, "UseGlobalEnergyScale", true),
105  m_viewContext( new FWViewContext()),
106  m_localEnergyScale( new FWViewEnergyScale(FWViewType::idToName(type), version)),
107  m_viewEnergyScaleEditor(0)
108 {
109  m_viewer = new TEveViewer(typeName().c_str());
110 
111  TGLEmbeddedViewer* embeddedViewer;
112 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,25,4)
113  embeddedViewer = m_viewer->SpawnGLEmbeddedViewer(0);
114 #else
115  embeddedViewer = m_viewer->SpawnGLEmbeddedViewer();
116 #endif
117  iParent->ReplaceWindow(m_viewer);
118  gEve->GetViewers()->AddElement(m_viewer);
119 
120  m_eventScene = gEve->SpawnNewScene(Form("EventScene %s", typeName().c_str()));
121  m_ownedProducts = new TEveElementList("ViewSpecificProducts");
122  m_eventScene->AddElement(m_ownedProducts);
123 
124  m_viewer->AddScene(m_eventScene);
125 
126  // spawn geo scene
127  m_geoScene = gEve->SpawnNewScene(Form("GeoScene %s", typeName().c_str()));
128  m_geoScene->GetGLScene()->SetSelectable(kFALSE);
129  m_viewer->AddScene(m_geoScene);
130 
131  FWGLEventHandler* eh = new FWGLEventHandler((TGWindow*)embeddedViewer->GetGLWidget(), (TObject*)embeddedViewer);
132  embeddedViewer->SetEventHandler(eh);
134  eh->SetDoInternalSelection(kFALSE);
136  // ctxHand->setPickCameraCenter(true);
137  m_viewContextMenu.reset(ctxHand);
138 
139  m_energyMaxValAnnotation = new ScaleAnnotation(viewerGL(), "empty", 0.1, 0.9);
140  m_energyMaxValAnnotation->SetRole(TGLOverlayElement::kViewer);
141  m_energyMaxValAnnotation->SetState(TGLOverlayElement::kInvisible);
142  m_energyMaxValAnnotation->SetUseColorSet(false);
143  m_energyMaxValAnnotation->SetTextSize(0.05);
144  m_energyMaxValAnnotation->SetTextColor(kMagenta);
145 
146  // style params
147 
148  m_overlayEventInfo = new FWEventAnnotation(embeddedViewer);
150 
151  m_eventInfoLevel.addEntry(0, "Nothing");
152  m_eventInfoLevel.addEntry(1, "Run / event");
153  m_eventInfoLevel.addEntry(2, "Run / event / lumi");
154  m_eventInfoLevel.addEntry(3, "Full");
156 
157  m_overlayLogo = new CmsAnnotation(embeddedViewer, 0.02, 0.98);
158  m_overlayLogo->setVisible(false);
160 
161  m_cameraGuide = new TGLCameraGuide(0.9, 0.1, 0.08);
162  m_cameraGuide->SetState(TGLOverlayElement::kInvisible);
163  embeddedViewer->AddOverlayElement(m_cameraGuide);
164  m_showCameraGuide.changed_.connect(boost::bind(&FWEveView::cameraGuideChanged,this));
165 
166  m_pointSmooth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
167  m_pointSize.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
168  m_lineSmooth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
169  m_lineWidth.changed_.connect(boost::bind(&FWEveView::pointLineScalesChanged,this));
172 
173 
174  // create scale for view ..
175  m_viewContext->setEnergyScale(m_localEnergyScale.get());
177  m_localEnergyScale->parameterChanged_.connect(boost::bind(&FWEveView::setupEnergyScale, this));
178 }
179 
181 {
182  m_geoScene->RemoveElements();
183  m_eventScene->RemoveElements();
184  m_viewer->DestroyWindowAndSlot();
185 }
186 
187 //______________________________________________________________________________
188 // const member functions
189 
190 
193  return dynamic_cast<FWViewContextMenuHandlerBase*> (m_viewContextMenu.get());
194 }
195 
196 TGLViewer*
198 {
199  return m_viewer->GetGLViewer();
200 }
201 
202 void
203 FWEveView::saveImageTo(const std::string& iName) const
204 {
205  bool succeeded = false;
206 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,0)
207  succeeded = viewerGL()->SavePictureScale(iName, m_imageScale.value());
208 #else
209  succeeded = viewerGL()->SavePicture(iName.c_str());
210 #endif
211 
212  if(!succeeded) {
213  throw std::runtime_error("Unable to save picture");
214  }
215  fwLog(fwlog::kInfo) << "Saved image " << iName << std::endl;
216 }
217 
218 //-------------------------------------------------------------------------------
219 void
221 {
222  viewerGL()->SetSmoothPoints(m_pointSmooth.value());
223  viewerGL()->SetPointScale (m_pointSize.value());
224  viewerGL()->SetSmoothLines (m_lineSmooth.value());
225  viewerGL()->SetLineScale (m_lineWidth.value());
226  viewerGL()->SetOLLineW (m_lineOutlineScale.value());
227  viewerGL()->SetWFLineW (m_lineWireframeScale.value());
228  viewerGL()->Changed();
229  gEve->Redraw3D();
230 }
231 
232 void
234 {
235  m_cameraGuide->SetBinaryState(m_showCameraGuide.value());
236  viewerGL()->Changed();
237  gEve->Redraw3D();
238 }
239 
240 void
242 {
243 }
244 
245 void
247 {
250 }
251 
252 void
254 {
256 }
257 
258 void
260 {
261  viewerGL()->ResetCurrentCamera();
262 }
263 
264 //______________________________________________________________________________
265 void
267 {
268  m_context = &x ;
269 
270  // in constructor view context has local scale
272  m_viewContext->setEnergyScale(context().commonPrefs()->getEnergyScale());
273 }
274 
275 bool
277 {
278  return m_useGlobalEnergyScale.value();
279 }
280 
281 void
283 {
287 }
288 
289 void
291 {
292  TEveCaloViz* calo = getEveCalo();
293  if (calo)
294  context().voteMaxEtAndEnergy(calo->GetData()->GetMaxVal(1), calo->GetData()->GetMaxVal(0));
295 }
296 
297 void
299 {
300  // Called at end of event OR if scale parameters changed.
301 
302  FWViewEnergyScale* energyScale = viewContext()->getEnergyScale();
303  // printf("setupEnergyScale %s >> scale name %s\n", typeName().c_str(), energyScale->name().c_str());
304  voteCaloMaxVal();
305 
306  // set cache for energy to lenght conversion
307  float maxVal = context().getMaxEnergyInEvent(energyScale->getPlotEt());
308  energyScale->updateScaleFactors(maxVal);
309  // printf("max event val %f \n", maxVal);
310  // printf("scales lego %f \n", energyScale->getScaleFactorLego());
311 
312  // configure TEveCaloViz
313  TEveCaloViz* calo = getEveCalo();
314  if (calo)
315  {
316  calo->SetPlotEt(energyScale->getPlotEt());
317  if (FWViewType::isLego(typeId()))
318  {
319  float f = energyScale->getScaleFactorLego();
320  calo->SetMaxValAbs(TMath::Pi()/f);
321  }
322  else
323  {
324  float f = energyScale->getScaleFactor3D();
325  calo->SetMaxValAbs(100/f);
326  }
327  calo->ElementChanged();
328  }
329 
330  // emit signal to proxy builders
332  gEve->Redraw3D();
333 }
334 
335 //-------------------------------------------------------------------------------
336 void
338 {
339  // take care of parameters
341 
342  {
343  assert ( m_overlayEventInfo );
345  }
346  {
347  assert ( m_overlayLogo );
348  m_overlayLogo->addTo(iTo);
349  }
350 
351  m_viewContext->getEnergyScale()->addTo(iTo);
352 }
353 
354 void
356 {
357  // Make sure you change the version ranges here
358  // whenever you update the logic.
359  // The rationale should be:
360  // (version range supported by the next block) && (version range in the configuration file)
361  //
362  // This is not "forward" compatible, but I don't think
363  // we care.
364  if (version() >= 2 && iFrom.version() >= 1)
365  {
366  for(const_iterator it =begin(), itEnd = end();
367  it != itEnd;
368  ++it) {
369  (*it)->setFrom(iFrom);
370  }
371  }
372  if (iFrom.version() > 1)
373  {
374  assert( m_overlayEventInfo);
375  m_overlayEventInfo->setFrom(iFrom);
376  }
377  {
378  assert( m_overlayLogo);
379  m_overlayLogo->setFrom(iFrom);
380  }
381 
382  if (iFrom.version() > 4)
383  {
384  m_localEnergyScale->setFrom(iFrom);
385  }
386 }
387 
388 //______________________________________________________________________________
389 
390 
391 void
392 FWEveView::addToOrthoCamera(TGLOrthoCamera* camera, FWConfiguration& iTo) const
393 {
394  // zoom
395  std::ostringstream s;
396  s<<(camera->fZoom);
397  std::string name("cameraZoom");
398  iTo.addKeyValue(name+typeName(),FWConfiguration(s.str()));
399 
400  // transformation matrix
401  std::string matrixName("cameraMatrix");
402  for ( unsigned int i = 0; i < 16; ++i ) {
403  std::ostringstream osIndex;
404  osIndex << i;
405  std::ostringstream osValue;
406  osValue << camera->GetCamTrans()[i];
407  iTo.addKeyValue(matrixName+osIndex.str()+typeName(),FWConfiguration(osValue.str()));
408  }
409 }
410 
411 void
412 FWEveView::setFromOrthoCamera(TGLOrthoCamera* camera, const FWConfiguration& iFrom)
413 {
414  try {
415  // zoom
416  std::string zoomName("cameraZoom"); zoomName += typeName();
417  if (iFrom.valueForKey(zoomName) == 0 )
418  {
419  throw std::runtime_error("can't restore parameter cameraZoom");
420  }
421  std::istringstream s(iFrom.valueForKey(zoomName)->value());
422  s>>(camera->fZoom);
423 
424  // transformation matrix
425  std::string matrixName("cameraMatrix");
426  for ( unsigned int i = 0; i < 16; ++i ) {
427  std::ostringstream os;
428  os << i;
429  const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + typeName() );
430  if ( value == 0 )
431  {
432  throw std::runtime_error ("can't restore parameter cameraMatrix.");
433  }
434  std::istringstream s(value->value());
435  s>> (camera->RefCamTrans()[i]);
436  }
437  }
438  catch (const std::runtime_error& iException)
439  {
440  fwLog(fwlog::kInfo) << "Caught exception while restoring camera parameters in view " << typeName() << "\n.";
441  viewerGL()->ResetCamerasAfterNextUpdate();
442 
443  }
444  camera->IncTimeStamp();
445 }
446 
447 void
448 FWEveView::addToPerspectiveCamera(TGLPerspectiveCamera* cam, const std::string& name, FWConfiguration& iTo) const
449 {
450  // transformation matrix
451  std::string matrixName("cameraMatrix");
452  for ( unsigned int i = 0; i < 16; ++i ){
453  std::ostringstream osIndex;
454  osIndex << i;
455  std::ostringstream osValue;
456  osValue << (cam->GetCamTrans())[i];
457  iTo.addKeyValue(matrixName+osIndex.str()+name,FWConfiguration(osValue.str()));
458  }
459 
460  // transformation matrix base
461  matrixName = "cameraMatrixBase";
462  for ( unsigned int i = 0; i < 16; ++i ){
463  std::ostringstream osIndex;
464  osIndex << i;
465  std::ostringstream osValue;
466  osValue << (cam->GetCamBase())[i];
467  iTo.addKeyValue(matrixName+osIndex.str()+name,FWConfiguration(osValue.str()));
468  }
469  {
470  std::ostringstream osValue;
471  osValue << cam->fFOV;
472  iTo.addKeyValue(name+" FOV",FWConfiguration(osValue.str()));
473  }
474 }
475 
476 void
477 FWEveView::setFromPerspectiveCamera(TGLPerspectiveCamera* cam, const std::string& name, const FWConfiguration& iFrom)
478 {
479  try {
480  std::string matrixName("cameraMatrix");
481  for ( unsigned int i = 0; i < 16; ++i ){
482  std::ostringstream os;
483  os << i;
484  const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + name );
485  if ( value == 0 )
486  {
487  throw std::runtime_error ("can't restore parameter cameraMatrix.");
488  }
489  std::istringstream s(value->value());
490  s>>((cam->RefCamTrans())[i]);
491  }
492 
493  // transformation matrix base
494  matrixName = "cameraMatrixBase";
495  for ( unsigned int i = 0; i < 16; ++i ){
496  std::ostringstream os;
497  os << i;
498  const FWConfiguration* value = iFrom.valueForKey( matrixName + os.str() + name );
499  if ( value == 0 )
500  {
501  throw std::runtime_error ("can't restore parameter cameraMatrixBase.");
502  }
503 
504  std::istringstream s(value->value());
505  s>>((cam->RefCamBase())[i]);
506  }
507 
508  {
509  const FWConfiguration* value = iFrom.valueForKey( name + " FOV" );
510  if ( value == 0 )
511  {
512  throw std::runtime_error ("can't restore parameter cameraMatrixBase.");
513  }
514  std::istringstream s(value->value());
515  s>>cam->fFOV;
516  }
517 
518  cam->IncTimeStamp();
519  }
520  catch (const std::runtime_error& iException)
521  {
522  fwLog(fwlog::kInfo) << "Caught exception while restoring camera parameters in view " << typeName() << "\n.";
523  viewerGL()->ResetCamerasAfterNextUpdate();
524  fwLog(fwlog::kDebug) << "Reset camera fo view " << typeName() << "\n.";
525  }
526 }
527 
528 
529 void
531 {
532  gui.requestTab("Style").
533  addParam(&m_eventInfoLevel).
534  addParam(&m_drawCMSLogo).
535  addParam(&m_showCameraGuide).
536  separator().
537 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,26,0)
538  addParam(&m_imageScale).
539 #endif
540  addParam(&m_pointSize).
541  addParam(&m_pointSmooth).
542  addParam(&m_lineSmooth).
543  addParam(&m_lineWidth).
544  addParam(&m_lineOutlineScale).
545  addParam(&m_lineWireframeScale);
546 
547 
548  gui.requestTab("Scales").
549  addParam(&m_useGlobalEnergyScale);
550 
554 }
void scaleChanged()
const double Pi
type
Definition: HCALResponse.h:22
float getScaleFactor3D() const
int i
Definition: DBlmapReader.cc:9
void addToOrthoCamera(TGLOrthoCamera *, FWConfiguration &) const
Definition: FWEveView.cc:392
FWEventAnnotation * m_overlayEventInfo
Definition: FWEveView.h:116
std::vector< FWParameterBase * >::const_iterator const_iterator
virtual TEveCaloViz * getEveCalo() const
Definition: FWEveView.h:95
list parent
Definition: dbtoconf.py:74
virtual ~FWEveView()
Definition: FWEveView.cc:180
virtual void useGlobalEnergyScaleChanged()
Definition: FWEveView.cc:282
const_iterator begin() const
std::auto_ptr< FWViewContext > m_viewContext
Definition: FWEveView.h:144
TEveViewer * m_viewer
Definition: FWEveView.h:111
virtual void populateController(ViewerParameterGUI &) const
Definition: FWEveView.cc:530
virtual void pointLineScalesChanged()
Definition: FWEveView.cc:220
void voteMaxEtAndEnergy(float Et, float energy) const
Definition: Context.cc:185
ScaleAnnotation(TGLViewerBase *parent, const char *text, Float_t posx, Float_t posy)
Definition: FWEveView.cc:66
ViewerParameterGUI & requestTab(const char *)
FWViewEnergyScale * getEnergyScale() const
virtual void setFrom(const FWConfiguration &)
Definition: FWEveView.cc:355
const std::string & typeName() const
Definition: FWViewBase.cc:114
virtual void setFrom(const FWConfiguration &)
TEveScene * m_geoScene
Definition: FWEveView.h:114
unsigned int version() const
FWViewEnergyScaleEditor * m_viewEnergyScaleEditor
Definition: FWEveView.h:147
virtual void setupEnergyScale()
Definition: FWEveView.cc:298
void addFrameToContainer(TGCompositeFrame *)
FWDoubleParameter m_pointSize
Definition: FWEveView.h:134
virtual FWViewContextMenuHandlerBase * contextMenuHandler() const
Definition: FWEveView.cc:192
TGLViewer * viewerGL() const
Definition: FWEveView.cc:197
void updateScaleFactors(float iMaxVal)
sigc::signal< void, T > changed_
const fireworks::Context * m_context
Definition: FWEveView.h:121
TEveElement * m_ownedProducts
Definition: FWEveView.h:113
FWBoolParameter m_lineSmooth
Definition: FWEveView.h:135
virtual void eventEnd()
Definition: FWEveView.cc:246
FWBoolParameter m_showCameraGuide
Definition: FWEveView.h:140
FWBoolParameter m_useGlobalEnergyScale
Definition: FWEveView.h:141
virtual void cameraGuideChanged()
Definition: FWEveView.cc:233
virtual void voteCaloMaxVal()
Definition: FWEveView.cc:290
virtual void setFrom(const FWConfiguration &)
virtual void saveImageTo(const std::string &iName) const
Definition: FWEveView.cc:203
ScaleAnnotation * m_energyMaxValAnnotation
Definition: FWEveView.h:118
void setText(const char *txt)
Definition: FWEveView.cc:70
float getScaleFactorLego() const
const_iterator end() const
bool addEntry(Long_t id, const std::string &txt)
double f[11][100]
tuple text
Definition: runonSM.py:42
float getMaxEnergyInEvent(bool isEt) const
Definition: Context.cc:201
virtual void resetCamera()
Definition: FWEveView.cc:259
virtual void addTo(FWConfiguration &) const
sigc::signal< void, Int_t, Int_t > openSelectedModelContextMenu_
FWBoolParameter m_drawCMSLogo
Definition: FWEveView.h:131
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
virtual void eventBegin()
Definition: FWEveView.cc:241
std::auto_ptr< FWViewEnergyScale > m_localEnergyScale
Definition: FWEveView.h:145
const std::string & value(unsigned int iIndex=0) const
const fireworks::Context & context()
Definition: FWEveView.h:65
FWBoolParameter m_pointSmooth
Definition: FWEveView.h:133
FWDoubleParameter m_imageScale
Definition: FWEveView.h:128
void setFromPerspectiveCamera(TGLPerspectiveCamera *, const std::string &, const FWConfiguration &)
Definition: FWEveView.cc:477
#define fwLog(_level_)
Definition: fwLog.h:51
FWViewContext * viewContext()
Definition: FWEveView.h:80
FWViewEnergyScale * getEnergyScale() const
Definition: CmsShowCommon.h:67
virtual ~ScaleAnnotation()
Definition: FWEveView.cc:68
virtual void setBackgroundColor(Color_t)
Definition: FWEveView.cc:253
FWEveView(TEveWindowSlot *, FWViewType::EType, unsigned int version=7)
Definition: FWEveView.cc:80
FWDoubleParameter m_lineOutlineScale
Definition: FWEveView.h:137
CmsShowCommon * commonPrefs() const
Definition: Context.cc:179
TGCompositeFrame * getTabContainer()
TEveScene * m_eventScene
Definition: FWEveView.h:112
FWEnumParameter m_eventInfoLevel
Definition: FWEveView.h:130
static bool isLego(int)
Definition: FWViewType.cc:135
static Bool_t setColorSetViewer(TGLViewer *, Color_t)
perl if(1 lt scalar(@::datatypes))
Definition: edlooper.cc:31
boost::shared_ptr< FWViewContextMenuHandlerGL > m_viewContextMenu
Definition: FWEveView.h:143
virtual void addTo(FWConfiguration &) const
const FWConfiguration * valueForKey(const std::string &iKey) const
string s
Definition: asciidump.py:422
Definition: DDAxes.h:10
virtual bool isEnergyScaleGlobal() const
Definition: FWEveView.cc:276
FWDoubleParameter m_lineWidth
Definition: FWEveView.h:136
FWDoubleParameter m_lineWireframeScale
Definition: FWEveView.h:138
void setFromOrthoCamera(TGLOrthoCamera *, const FWConfiguration &)
Definition: FWEveView.cc:412
virtual void addTo(FWConfiguration &) const
Definition: FWEveView.cc:337
void setVisible(bool x)
sigc::signal< void, Int_t, Int_t > openSelectedModelContextMenu_
Definition: FWViewBase.h:57
TGLCameraGuide * m_cameraGuide
Definition: FWEveView.h:119
virtual void setContext(const fireworks::Context &x)
Definition: FWEveView.cc:266
bool getPlotEt() const
CmsAnnotation * m_overlayLogo
Definition: FWEveView.h:117
virtual void addTo(FWConfiguration &) const
FWViewType::EType typeId() const
Definition: FWViewBase.h:42
void addToPerspectiveCamera(TGLPerspectiveCamera *, const std::string &, FWConfiguration &) const
Definition: FWEveView.cc:448