CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/Fireworks/Calo/plugins/FWJetProxyBuilder.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Calo
00004 // Class  :     FWJetProxyBuilder
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Tue Dec  2 14:17:03 EST 2008
00011 // $Id: FWJetProxyBuilder.cc,v 1.35 2011/11/21 08:36:37 amraktad Exp $
00012 //
00013 
00014 #include "TEveJetCone.h"
00015 #include "TEveScalableStraightLineSet.h"
00016 
00017 #include "Fireworks/Core/interface/FWTextProjected.h"
00018 #include "Fireworks/Core/interface/FWEventItem.h"
00019 #include "Fireworks/Core/interface/FWProxyBuilderConfiguration.h"
00020 #include "Fireworks/Core/interface/FWParameters.h"
00021 // user include files
00022 #include "Fireworks/Core/interface/FWSimpleProxyBuilderTemplate.h"
00023 #include "Fireworks/Core/interface/FWEventItem.h"
00024 #include "Fireworks/Core/interface/Context.h"
00025 #include "Fireworks/Core/interface/FWViewContext.h"
00026 #include "Fireworks/Core/interface/FWViewEnergyScale.h"
00027 #include "Fireworks/Calo/interface/makeEveJetCone.h"
00028 #include "Fireworks/Calo/interface/scaleMarker.h"
00029 
00030 #include "DataFormats/JetReco/interface/Jet.h"
00031 
00032 namespace fireworks {
00033 
00034 struct jetScaleMarker : public  scaleMarker {
00035    jetScaleMarker(TEveScalableStraightLineSet* ls, float et, float e, const FWViewContext* vc):
00036       scaleMarker(ls, et, e, vc) , m_text(0) {}
00037    
00038    FWEveText* m_text;
00039 };
00040 }
00041 
00042 static const std::string kJetLabelsRhoPhiOn("Draw Labels in RhoPhi View");
00043 static const std::string kJetLabelsRhoZOn("Draw Labels in RhoZ View");
00044 static const std::string kJetOffset("Label Offset");
00045 
00046 
00047 class FWJetProxyBuilder : public FWSimpleProxyBuilderTemplate<reco::Jet>
00048 {
00049 public:
00050    FWJetProxyBuilder();
00051    virtual ~FWJetProxyBuilder();
00052 
00053    virtual bool havePerViewProduct(FWViewType::EType) const { return true; }
00054    virtual bool haveSingleProduct() const { return false; } // different view types
00055    virtual void cleanLocal();
00056 
00057    virtual void setItem(const FWEventItem* iItem)
00058    {
00059       FWProxyBuilderBase::setItem(iItem);
00060       if (iItem) {
00061       iItem->getConfig()->assertParam(kJetLabelsRhoPhiOn, false);
00062       iItem->getConfig()->assertParam(kJetLabelsRhoZOn, false);
00063       iItem->getConfig()->assertParam(kJetOffset, 2.1, 1.0, 5.0);
00064       }
00065    }
00066 
00067    REGISTER_PROXYBUILDER_METHODS();
00068    
00069 protected:
00070    virtual void buildViewType(const reco::Jet& iData, unsigned int iIndex, TEveElement& oItemHolder, FWViewType::EType type , const FWViewContext*);
00071 
00072 
00073    virtual void localModelChanges(const FWModelId& iId, TEveElement* iCompound,
00074                                   FWViewType::EType viewType, const FWViewContext* vc);
00075 
00076    virtual void scaleProduct(TEveElementList* parent, FWViewType::EType, const FWViewContext* vc);
00077 
00078 private:
00079    typedef std::vector<fireworks::jetScaleMarker> Lines_t;  
00080 
00081    FWJetProxyBuilder( const FWJetProxyBuilder& ); // stop default
00082    const FWJetProxyBuilder& operator=( const FWJetProxyBuilder& ); // stop default
00083 
00084    TEveElementList* requestCommon();
00085    void setTextPos(fireworks::jetScaleMarker& s, const FWViewContext* vc, FWViewType::EType);
00086 
00087    TEveElementList* m_common;
00088 
00089    std::vector<fireworks::jetScaleMarker> m_lines;
00090 };
00091 
00092 
00093 //______________________________________________________________________________
00094 FWJetProxyBuilder::FWJetProxyBuilder():
00095    m_common(0)
00096 {
00097    m_common = new TEveElementList( "common electron scene" );
00098    m_common->IncDenyDestroy();
00099 }
00100 
00101 FWJetProxyBuilder::~FWJetProxyBuilder()
00102 {
00103    m_common->DecDenyDestroy();
00104 }
00105 
00106 TEveElementList*
00107 FWJetProxyBuilder::requestCommon()
00108 {
00109    if( m_common->HasChildren() == false )
00110    {
00111       for (int i = 0; i < static_cast<int>(item()->size()); ++i)
00112       {
00113          TEveJetCone* cone = fireworks::makeEveJetCone(modelData(i), context());
00114 
00115          m_common->AddElement(cone);
00116          cone->SetFillColor(item()->defaultDisplayProperties().color());
00117          cone->SetLineColor(item()->defaultDisplayProperties().color());
00118 
00119       }
00120    }
00121    return m_common;
00122 }
00123 
00124 void
00125 FWJetProxyBuilder::buildViewType(const reco::Jet& iData, unsigned int iIndex, TEveElement& oItemHolder, FWViewType::EType type , const FWViewContext* vc)
00126 {
00127    // add cone from shared pool
00128    TEveElementList*    cones = requestCommon();
00129    TEveElement::List_i coneIt = cones->BeginChildren();
00130    std::advance(coneIt, iIndex);
00131 
00132    const FWDisplayProperties &dp = item()->defaultDisplayProperties();
00133    setupAddElement( *coneIt, &oItemHolder );
00134    (*coneIt)->SetMainTransparency(TMath::Min(100, 80 + dp.transparency() / 5)); 
00135 
00136 
00137    // scale markers in projected views
00138    if (FWViewType::isProjected(type))
00139    {
00140       m_lines.push_back(fireworks::jetScaleMarker(new TEveScalableStraightLineSet("jetline"), iData.et(), iData.energy(), vc));
00141       fireworks::jetScaleMarker& markers =  m_lines.back();
00142 
00143       float size = 1.f; // values are saved in scale
00144       double theta = iData.theta();
00145       double phi = iData.phi();
00146 
00147       if ( type == FWViewType::kRhoZ )
00148       {
00149          static const float_t offr = 4;
00150          float r_ecal = context().caloR1() + offr;
00151          float z_ecal = context().caloZ1() + offr/tan(context().caloTransAngle());
00152          double r(0);
00153          if ( theta < context().caloTransAngle() || M_PI-theta < context().caloTransAngle())
00154          {
00155             z_ecal = context().caloZ2() + offr/tan(context().caloTransAngle());
00156             r = z_ecal/fabs(cos(theta));
00157          }
00158          else
00159          {
00160             r = r_ecal/sin(theta);
00161          }
00162 
00163          markers.m_ls->SetScaleCenter( 0., (phi>0 ? r*fabs(sin(theta)) : -r*fabs(sin(theta))), r*cos(theta) );
00164          markers.m_ls->AddLine( 0., (phi>0 ? r*fabs(sin(theta)) : -r*fabs(sin(theta))), r*cos(theta),
00165                                 0., (phi>0 ? (r+size)*fabs(sin(theta)) : -(r+size)*fabs(sin(theta))), (r+size)*cos(theta) );
00166       }
00167       else
00168       {
00169          float ecalR = context().caloR1() + 4;
00170          markers.m_ls->SetScaleCenter(ecalR*cos(phi), ecalR*sin(phi), 0);
00171          markers.m_ls->AddLine(ecalR*cos(phi), ecalR*sin(phi), 0, (ecalR+size)*cos(phi), (ecalR+size)*sin(phi), 0);
00172       }
00173 
00174       markers.m_ls->SetLineWidth(4);  
00175       markers.m_ls->SetLineColor(dp.color());
00176       FWViewEnergyScale* caloScale = vc->getEnergyScale();    
00177       markers.m_ls->SetScale(caloScale->getScaleFactor3D()*(caloScale->getPlotEt() ?  iData.et() : iData.energy()));
00178 
00179       if ((type == FWViewType::kRhoZ && item()->getConfig()->value<bool>(kJetLabelsRhoZOn))||
00180           (type == FWViewType::kRhoPhi && item()->getConfig()->value<bool>(kJetLabelsRhoPhiOn) ) )
00181       {
00182          markers.m_text = new FWEveText(Form("%.1f", vc->getEnergyScale()->getPlotEt() ? iData.et() : iData.energy()));
00183          markers.m_text->SetMainColor( item()->defaultDisplayProperties().color());         
00184          setTextPos(markers, vc, type);
00185       }
00186 
00187 
00188       markers.m_ls->SetMarkerColor(markers.m_ls->GetMainColor());
00189       setupAddElement( markers.m_ls, &oItemHolder );
00190       if (markers.m_text)  setupAddElement( markers.m_text, &oItemHolder , false);
00191 
00192    }
00193    context().voteMaxEtAndEnergy(iData.et(), iData.energy());
00194  
00195 }
00196 
00197 void
00198 FWJetProxyBuilder::localModelChanges(const FWModelId& iId, TEveElement* iCompound,
00199                                      FWViewType::EType viewType, const FWViewContext* vc)
00200 {
00201    increaseComponentTransparency(iId.index(), iCompound, "TEveJetCone", 80);
00202 
00203    for (Lines_t::iterator i = m_lines.begin(); i!= m_lines.end(); ++ i)
00204    {
00205       TEveStraightLineSetProjected* projLineSet = (TEveStraightLineSetProjected*)(*(*i).m_ls->BeginProjecteds());
00206       if (projLineSet) projLineSet->UpdateProjection();
00207    }
00208 }
00209 
00210 
00211 void
00212 FWJetProxyBuilder::cleanLocal()
00213 {
00214    m_lines.clear();
00215    m_common->DestroyElements();
00216 }
00217 
00218 void
00219 FWJetProxyBuilder::scaleProduct(TEveElementList* parent, FWViewType::EType type, const FWViewContext* vc)
00220 { 
00221    for (Lines_t::iterator i = m_lines.begin(); i!= m_lines.end(); ++ i)
00222    {
00223       if (vc == (*i).m_vc)
00224       {  
00225          float value = vc->getEnergyScale()->getPlotEt() ? (*i).m_et : (*i).m_energy;
00226 
00227          (*i).m_ls->SetScale(vc->getEnergyScale()->getScaleFactor3D() * value );
00228          if ((*i).m_text) 
00229          {
00230             (*i).m_text->SetText(Form("%.1f", value));
00231             setTextPos(*i, vc, type);
00232          }
00233          TEveStraightLineSetProjected* projLineSet = (TEveStraightLineSetProjected*)(*(*i).m_ls->BeginProjecteds());
00234          projLineSet->UpdateProjection();
00235       }
00236    }
00237 }
00238 
00239 
00240 void FWJetProxyBuilder::setTextPos(fireworks::jetScaleMarker& s, const FWViewContext* vc, FWViewType::EType type)
00241 {   
00242    TEveChunkManager::iterator li( s.m_ls->GetLinePlex() );
00243    li.next();
00244    TEveStraightLineSet::Line_t &l = * ( TEveStraightLineSet::Line_t* ) li();
00245    TEveVector v(l.fV2[0] - l.fV1[0], l.fV2[1] - l.fV1[1],  l.fV2[2] - l.fV1[2] );
00246    v.Normalize();
00247 
00248 
00249    double off = item()->getConfig()->value<double>(kJetOffset) -1;
00250    float value = vc->getEnergyScale()->getPlotEt() ? s.m_et : s.m_energy;
00251    double trs = off * 130 * value/context().getMaxEnergyInEvent(vc->getEnergyScale()->getPlotEt());
00252    v *= trs;
00253 
00254    float x = l.fV1[0] + v[0];
00255    float y = l.fV1[1] + v[1];
00256    float z = l.fV1[2] + v[2];
00257 
00258    s.m_text->m_offsetZ =  value/context().getMaxEnergyInEvent(vc->getEnergyScale()->getPlotEt());
00259    s.m_text->RefMainTrans().SetPos(x, y, z);
00260    if ((s.m_text)->BeginProjecteds() != (s.m_text)->EndProjecteds()) {
00261       FWEveTextProjected* textProjected = (FWEveTextProjected*)(*(s.m_text)->BeginProjecteds());
00262       textProjected->UpdateProjection();
00263    }
00264    
00265 }
00266 
00267 REGISTER_FWPROXYBUILDER( FWJetProxyBuilder, reco::Jet, "Jets", FWViewType::kAll3DBits  | FWViewType::kAllRPZBits | FWViewType::kGlimpseBit);