Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:42

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWRPZView
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Tue Feb 19 10:33:25 EST 2008
0011 //
0012 
0013 // system include files
0014 #include <stdexcept>
0015 #include <functional>
0016 #include <memory>
0017 
0018 #include "TGLViewer.h"
0019 #include "TGLScenePad.h"
0020 #include "TEveManager.h"
0021 #include "TEveElement.h"
0022 #include "TEveScene.h"
0023 #include "TEveProjections.h"
0024 #include "TEveProjectionAxes.h"
0025 #include "TGLabel.h"
0026 #include "TEveProjectionManager.h"
0027 #include "TEveCalo.h"
0028 
0029 // user include files
0030 #include "Fireworks/Core/interface/FWRPZView.h"
0031 #include "Fireworks/Core/interface/FWRPZViewGeometry.h"
0032 #include "Fireworks/Core/interface/FWBeamSpot.h"
0033 #include "Fireworks/Core/interface/Context.h"
0034 #include "Fireworks/Core/interface/fwLog.h"
0035 #include "Fireworks/Core/interface/FWViewContext.h"
0036 #include "Fireworks/Core/interface/FWViewContext.h"
0037 #include "Fireworks/Core/interface/FWViewEnergyScale.h"
0038 #include "Fireworks/Core/interface/CmsShowViewPopup.h"
0039 #include "Fireworks/Core/interface/CmsShowCommon.h"
0040 
0041 const float FWRPZView::s_distortF = 0.001;
0042 const float FWRPZView::s_distortFInv = 1000;
0043 //
0044 // constructors and destructor
0045 //
0046 FWRPZView::FWRPZView(TEveWindowSlot* iParent, FWViewType::EType id)
0047     : FWEveView(iParent, id, 7),
0048       m_geometryList(nullptr),
0049       m_projMgr(nullptr),
0050       m_axes(nullptr),
0051 
0052       m_calo(nullptr),
0053 
0054       m_showPixelBarrel(this, "Show Pixel Barrel", false),
0055       m_showPixelEndcap(this, "Show Pixel Endcap", false),
0056       m_showTrackerBarrel(this, "Show Tracker Barrel", false),
0057       m_showTrackerEndcap(this, "Show Tracker Endcap", false),
0058       m_showRpcEndcap(this, "Show RPC Endcap", false),
0059       m_showGEM(this, "Show GEM", false),
0060       m_showME0(this, "Show ME0", false),
0061 
0062       m_showMtdBarrel(this, "Show MTD Barrel", false),
0063       m_showMtdEndcap(this, "Show MTD Endcap", false),
0064 
0065       m_shiftOrigin(this, "Shift origin to beam-spot", false),
0066       m_fishEyeDistortion(this, "Distortion", 0., 0., 100.),
0067       m_fishEyeR(this, "FixedRadius", (double)fireworks::Context::caloR1(), 0.0, 150.0),
0068 
0069       m_caloDistortion(this, "Calo compression", 1.0, 0.01, 10.),
0070       m_muonDistortion(this, "Muon compression", 0.2, 0.01, 10.),
0071       m_showProjectionAxes(this, "Show projection axis", false),
0072       m_projectionAxesLabelSize(this, "Projection axis label size", 0.015, 0.001, 0.2),
0073       m_compressMuon(this, "Compress detectors", false),
0074       m_showHF(nullptr),
0075       m_showEndcaps(nullptr) {
0076   TEveProjection::EPType_e projType = (id == FWViewType::kRhoZ) ? TEveProjection::kPT_RhoZ : TEveProjection::kPT_RPhi;
0077 
0078   m_projMgr = new TEveProjectionManager(projType);
0079   m_projMgr->IncDenyDestroy();
0080   m_projMgr->SetImportEmpty(kTRUE);
0081 
0082   m_projMgr->GetProjection()->SetDistortion(m_fishEyeDistortion.value() * s_distortF);
0083   m_projMgr->GetProjection()->SetFixR(m_fishEyeR.value());
0084 
0085 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
0086   m_projMgr->GetProjection()->SetDisplaceOrigin(m_shiftOrigin.value());
0087 #endif
0088 
0089   if (id == FWViewType::kRhoPhi || id == FWViewType::kRhoPhiPF) {
0090     m_projMgr->GetProjection()->AddPreScaleEntry(0, fireworks::Context::caloR1(), 1.0);
0091     m_projMgr->GetProjection()->AddPreScaleEntry(0, 300, 0.2);
0092   } else {
0093     m_projMgr->GetProjection()->AddPreScaleEntry(0, fireworks::Context::caloR1(), 1.0);
0094     m_projMgr->GetProjection()->AddPreScaleEntry(1, 310, 1.0);
0095     m_projMgr->GetProjection()->AddPreScaleEntry(0, 370, 0.2);
0096     m_projMgr->GetProjection()->AddPreScaleEntry(1, 580, 0.2);
0097   }
0098 
0099   // camera
0100   viewerGL()->SetCurrentCamera(TGLViewer::kCameraOrthoXOY);
0101   if (TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>(&(viewerGL()->CurrentCamera()))) {
0102     camera->SetZoomMax(1e6);
0103   }
0104   geoScene()->GetGLScene()->SetSelectable(kFALSE);
0105 
0106   m_axes = new TEveProjectionAxes(m_projMgr);
0107   m_axes->SetRnrState(m_showProjectionAxes.value());
0108   m_axes->SetLabelSize(m_projectionAxesLabelSize.value());
0109   m_showProjectionAxes.changed_.connect(std::bind(&FWRPZView::showProjectionAxes, this));
0110   m_projectionAxesLabelSize.changed_.connect(std::bind(&FWRPZView::projectionAxesLabelSize, this));
0111   eventScene()->AddElement(m_axes);
0112 
0113   if (id != FWViewType::kRhoZ) {
0114     m_showEndcaps = new FWBoolParameter(this, "Include EndCaps", true);
0115     m_showEndcaps->changed_.connect(std::bind(&FWRPZView::setEtaRng, this));
0116     m_showHF = new FWBoolParameter(this, "Include HF", true);
0117     m_showHF->changed_.connect(std::bind(&FWRPZView::setEtaRng, this));
0118   }
0119 
0120   m_shiftOrigin.changed_.connect(std::bind(&FWRPZView::doShiftOriginToBeamSpot, this));
0121 
0122   m_fishEyeDistortion.changed_.connect(std::bind(&FWRPZView::doFishEyeDistortion, this));
0123 
0124   m_fishEyeR.changed_.connect(std::bind(&FWRPZView::doFishEyeDistortion, this));
0125 
0126   m_caloDistortion.changed_.connect(std::bind(&FWRPZView::doPreScaleDistortion, this));
0127   m_muonDistortion.changed_.connect(std::bind(&FWRPZView::doPreScaleDistortion, this));
0128   m_compressMuon.changed_.connect(std::bind(&FWRPZView::doCompression, this, std::placeholders::_1));
0129 }
0130 
0131 FWRPZView::~FWRPZView() {
0132   m_calo->Destroy();
0133   m_projMgr->DecDenyDestroy();
0134 }
0135 
0136 //
0137 // member functions
0138 //
0139 
0140 TEveCaloViz* FWRPZView::getEveCalo() const { return static_cast<TEveCaloViz*>(m_calo); }
0141 
0142 void FWRPZView::setContext(const fireworks::Context& ctx) {
0143   FWEveView::setContext(ctx);
0144 
0145   m_geometryList = new FWRPZViewGeometry(ctx);
0146   m_geometryList->IncDenyDestroy();
0147   m_geometryList->initStdGeoElements(typeId());
0148   TEveElement* p = m_projMgr->ImportElements(m_geometryList);
0149   geoScene()->AddElement(p);
0150 
0151   TEveCaloData* data = context().getCaloData();
0152 
0153   TEveCalo3D* calo3d = new TEveCalo3D(data);
0154 
0155   m_calo = static_cast<TEveCalo2D*>(m_projMgr->ImportElements(calo3d, eventScene()));
0156 
0157   if (typeId() == FWViewType::kRhoPhiPF)
0158     m_calo->SetBarrelRadius(177);
0159   else
0160     m_calo->SetBarrelRadius(context().caloR1(false));
0161 
0162   m_calo->SetEndCapPos(context().caloZ1(false));
0163   m_calo->SetAutoRange(false);
0164   m_calo->SetScaleAbs(true);
0165 
0166   m_showPixelBarrel.changed_.connect(
0167       std::bind(&FWRPZViewGeometry::showPixelBarrel, m_geometryList, std::placeholders::_1));
0168   m_showPixelEndcap.changed_.connect(
0169       std::bind(&FWRPZViewGeometry::showPixelEndcap, m_geometryList, std::placeholders::_1));
0170   m_showTrackerBarrel.changed_.connect(
0171       std::bind(&FWRPZViewGeometry::showTrackerBarrel, m_geometryList, std::placeholders::_1));
0172   m_showTrackerEndcap.changed_.connect(
0173       std::bind(&FWRPZViewGeometry::showTrackerEndcap, m_geometryList, std::placeholders::_1));
0174   m_showRpcEndcap.changed_.connect(std::bind(&FWRPZViewGeometry::showRpcEndcap, m_geometryList, std::placeholders::_1));
0175   m_showGEM.changed_.connect(std::bind(&FWRPZViewGeometry::showGEM, m_geometryList, std::placeholders::_1));
0176   m_showME0.changed_.connect(std::bind(&FWRPZViewGeometry::showME0, m_geometryList, std::placeholders::_1));
0177   m_showMtdBarrel.changed_.connect(std::bind(&FWRPZViewGeometry::showMtdBarrel, m_geometryList, std::placeholders::_1));
0178   m_showMtdEndcap.changed_.connect(std::bind(&FWRPZViewGeometry::showMtdEndcap, m_geometryList, std::placeholders::_1));
0179 }
0180 
0181 void FWRPZView::eventBegin() {
0182   if (context().getBeamSpot()) {
0183     float c[3] = {0, 0, 0};
0184     context().commonPrefs()->getEventCenter(c);
0185 
0186     // projection center
0187     TEveVector center(c[0], c[1], c[2]);
0188     m_projMgr->GetProjection()->SetCenter(center);
0189 
0190     // camera move
0191     TGLCamera& cam = viewerGL()->CurrentCamera();
0192     cam.SetExternalCenter(true);
0193     if (typeId() != FWViewType::kRhoZ) {
0194       double r = center.Mag();
0195       cam.SetCenterVec(center.fZ, TMath::Sign(r, center.fY), 0);
0196     } else {
0197       cam.SetCenterVec(c[0], c[1], c[2]);
0198     }
0199   }
0200 }
0201 
0202 void FWRPZView::eventEnd() {
0203   float c[3];
0204   context().commonPrefs()->getEventCenter(c);
0205   m_projMgr->SetCenter(c[0], c[1], c[2]);
0206   FWEveView::eventEnd();
0207 }
0208 
0209 void FWRPZView::setupEventCenter() {
0210   float c[3];
0211   context().commonPrefs()->getEventCenter(c);
0212   m_projMgr->SetCenter(c[0], c[1], c[2]);
0213   FWEveView::setupEventCenter();
0214 }
0215 
0216 void FWRPZView::doShiftOriginToBeamSpot() {
0217 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
0218 
0219   TEveProjection* p = m_projMgr->GetProjection();
0220   if (p->GetDisplaceOrigin() != m_shiftOrigin.value()) {
0221     p->SetDisplaceOrigin(m_shiftOrigin.value());
0222     m_projMgr->ProjectChildren();
0223     gEve->Redraw3D();
0224   }
0225 #endif
0226 }
0227 
0228 void FWRPZView::shiftOrigin(TEveVector& center) {
0229 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
0230   // re-project with new center
0231   m_projMgr->GetProjection()->SetCenter(center);
0232   m_projMgr->ProjectChildren();
0233 
0234   // draw projected center
0235   float* pc = m_projMgr->GetProjection()->GetProjectedCenter();
0236   viewerGL()->CurrentCamera().SetExternalCenter(true);
0237   viewerGL()->CurrentCamera().SetCenterVec(pc[0], pc[1], pc[2]);
0238   viewerGL()->SetDrawCameraCenter(true);
0239 
0240   gEve->Redraw3D();
0241 #endif
0242 }
0243 
0244 void FWRPZView::resetOrigin() {
0245   // set center back to beam spot
0246 
0247 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
0248   FWBeamSpot& b = *(context().getBeamSpot());
0249   TEveVector center(b.x0(), b.y0(), b.z0());
0250   m_projMgr->GetProjection()->SetCenter(center);
0251 
0252   m_projMgr->ProjectChildren();
0253   gEve->Redraw3D();
0254 #endif
0255 }
0256 
0257 void FWRPZView::doFishEyeDistortion() {
0258   TEveProjection* p = m_projMgr->GetProjection();
0259   if (p->GetDistortion() != m_fishEyeDistortion.value() * s_distortFInv)
0260     p->SetDistortion(m_fishEyeDistortion.value() * s_distortF);
0261   if (p->GetFixR() != m_fishEyeR.value())
0262     p->SetFixR(m_fishEyeR.value());
0263 
0264   m_projMgr->ProjectChildren();
0265   gEve->Redraw3D();
0266 }
0267 
0268 void FWRPZView::doPreScaleDistortion() {
0269   if (typeId() == FWViewType::kRhoPhi || typeId() == FWViewType::kRhoPhiPF) {
0270     m_projMgr->GetProjection()->ChangePreScaleEntry(0, 1, m_caloDistortion.value());
0271     m_projMgr->GetProjection()->ChangePreScaleEntry(0, 2, m_muonDistortion.value());
0272   } else {
0273     m_projMgr->GetProjection()->ChangePreScaleEntry(0, 1, m_caloDistortion.value());
0274     m_projMgr->GetProjection()->ChangePreScaleEntry(0, 2, m_muonDistortion.value());
0275     m_projMgr->GetProjection()->ChangePreScaleEntry(1, 1, m_caloDistortion.value());
0276     m_projMgr->GetProjection()->ChangePreScaleEntry(1, 2, m_muonDistortion.value());
0277   }
0278   m_projMgr->UpdateName();
0279   m_projMgr->ProjectChildren();
0280   gEve->Redraw3D();
0281 }
0282 
0283 void FWRPZView::doCompression(bool flag) {
0284   m_projMgr->GetProjection()->SetUsePreScale(flag);
0285   m_projMgr->UpdateName();
0286   m_projMgr->ProjectChildren();
0287   gEve->Redraw3D();
0288 }
0289 
0290 void FWRPZView::importElements(TEveElement* iChildren, float iLayer, TEveElement* iProjectedParent) {
0291   float oldLayer = m_projMgr->GetCurrentDepth();
0292   m_projMgr->SetCurrentDepth(iLayer);
0293   //make sure current depth is reset even if an exception is thrown
0294   std::shared_ptr<TEveProjectionManager> sentry(
0295       m_projMgr, std::bind(&TEveProjectionManager::SetCurrentDepth, std::placeholders::_1, oldLayer));
0296   m_projMgr->ImportElements(iChildren, iProjectedParent);
0297 }
0298 
0299 void FWRPZView::addTo(FWConfiguration& iTo) const {
0300   FWEveView::addTo(iTo);
0301   TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>(&(viewerGL()->CurrentCamera()));
0302   if (camera)
0303     addToOrthoCamera(camera, iTo);
0304 }
0305 
0306 void FWRPZView::setFrom(const FWConfiguration& iFrom) {
0307   FWEveView::setFrom(iFrom);
0308 
0309   TGLOrthoCamera* camera = dynamic_cast<TGLOrthoCamera*>(&(viewerGL()->CurrentCamera()));
0310   if (camera)
0311     setFromOrthoCamera(camera, iFrom);
0312 
0313   if (iFrom.version() < 7) {
0314     const FWConfiguration* value = iFrom.valueForKey("Show projection axes");
0315     if (value)
0316       m_showProjectionAxes.set(value->value() == "1");
0317   }
0318 }
0319 
0320 void FWRPZView::setEtaRng() {
0321   if (typeId() != FWViewType::kRhoZ) {
0322     // rng controllers only in RhoPhi
0323     double eta_range = context().caloMaxEta();
0324     if (!m_showHF->value())
0325       eta_range = 3.0;
0326     if (!m_showEndcaps->value())
0327       eta_range = context().caloTransEta();
0328     m_calo->SetEta(-eta_range, eta_range);
0329   }
0330 
0331   FWEveView::setupEnergyScale();
0332 }
0333 
0334 void FWRPZView::voteCaloMaxVal() {
0335   if (!m_calo->GetData()->Empty()) {
0336     m_calo->AssertCellIdCache();
0337     Float_t sumEt, sumE;
0338     TEveCaloData::CellData_t cellData;
0339     typedef std::vector<TEveCaloData::vCellId_t*> vBinCells_t;
0340     typedef std::vector<TEveCaloData::vCellId_t*>::iterator vBinCells_i;
0341 
0342     vBinCells_t cellLists = m_calo->GetBinLists();
0343     for (vBinCells_i it = cellLists.begin(); it != cellLists.end(); it++) {
0344       TEveCaloData::vCellId_t* binCells = *it;
0345       if (binCells) {
0346         sumEt = 0;
0347         sumE = 0;
0348         TEveCaloData::vCellId_i a = binCells->end();
0349 
0350         for (TEveCaloData::vCellId_i k = binCells->begin(); k != a; ++k) {
0351           m_calo->GetData()->GetCellData((*k), cellData);
0352           sumEt += cellData.Value(true);
0353           sumE += cellData.Value(false);
0354         }
0355         // printf("vote sum %f %f \n", sumEt , sumE);
0356         context().voteMaxEtAndEnergy(sumEt, sumE);
0357       }
0358     }
0359   }
0360 }
0361 
0362 void FWRPZView::showProjectionAxes() {
0363   m_axes->SetRnrState(m_showProjectionAxes.value());
0364   gEve->Redraw3D();
0365   viewerGL()->RequestDraw();
0366 }
0367 
0368 void FWRPZView::projectionAxesLabelSize() {
0369   m_axes->SetLabelSize(m_projectionAxesLabelSize.value());
0370   viewerGL()->RequestDraw();
0371 }
0372 
0373 void FWRPZView::populateController(ViewerParameterGUI& gui) const {
0374   FWEveView::populateController(gui);
0375 
0376   ViewerParameterGUI& det = gui.requestTab("Detector");
0377   ;
0378   det.addParam(&m_showPixelBarrel);
0379 
0380   if (typeId() == FWViewType::kRhoZ) {
0381     det.addParam(&m_showTrackerBarrel);
0382     det.addParam(&m_showPixelEndcap);
0383     det.addParam(&m_showRpcEndcap);
0384     bool showGEM = m_context->getGeom()->versionInfo().haveExtraDet("GEM");
0385     if (showGEM)
0386       det.addParam(&m_showGEM);
0387     bool showME0 = m_context->getGeom()->versionInfo().haveExtraDet("ME0");
0388     if (showME0)
0389       det.addParam(&m_showME0);
0390   }
0391 
0392   det.addParam(&m_showMtdBarrel);
0393   if (typeId() == FWViewType::kRhoZ)
0394     det.addParam(&m_showMtdEndcap);
0395 
0396 #ifdef TEVEPROJECTIONS_DISPLACE_ORIGIN_MODE
0397   gui.requestTab("Projection").addParam(&m_shiftOrigin);
0398 #endif
0399 
0400   gui.requestTab("Projection").addParam(&m_showProjectionAxes).addParam(&m_projectionAxesLabelSize).separator();
0401 
0402   TGCompositeFrame* f = gui.getTabContainer();
0403 
0404   f->AddFrame(new TGLabel(f, "FishEye:"));
0405   gui.addParam(&m_fishEyeDistortion).addParam(&m_fishEyeR).separator();
0406 
0407   f->AddFrame(new TGLabel(f, "PreScales:"));
0408 
0409   gui.requestTab("Projection").addParam(&m_compressMuon).addParam(&m_muonDistortion).addParam(&m_caloDistortion);
0410 
0411   if (typeId() == FWViewType::kRhoPhi || typeId() == FWViewType::kRhoPhiPF) {
0412     gui.requestTab("Calo").addParam(m_showHF).addParam(m_showEndcaps);
0413   }
0414 }