CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/Fireworks/Calo/plugins/FWHFTowerProxyBuilder.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Calo
00004 // Class  :     FWHFTowerProxyBuilder
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  
00010 //         Created:  Mon May 31 16:41:27 CEST 2010
00011 // $Id: FWHFTowerProxyBuilder.cc,v 1.22 2011/09/06 15:07:30 yana Exp $
00012 //
00013 
00014 // system include files
00015 
00016 // user include files
00017 #include "TEveCaloData.h"
00018 #include "TEveCalo.h"
00019 #include "TH2F.h"
00020 
00021 #include "Fireworks/Calo/plugins/FWHFTowerProxyBuilder.h"
00022 #include "Fireworks/Calo/plugins/FWHFTowerSliceSelector.h"
00023 #include "Fireworks/Core/interface/Context.h"
00024 #include "Fireworks/Core/interface/FWEventItem.h"
00025 #include "Fireworks/Core/interface/FWGeometry.h"
00026 #include "Fireworks/Core/interface/FWModelChangeManager.h"
00027 #include "Fireworks/Core/interface/fwLog.h"
00028 
00029 FWHFTowerProxyBuilderBase::FWHFTowerProxyBuilderBase():
00030    m_hits(0),
00031    // m_depth(depth),
00032    m_vecData(0)
00033 {}
00034 
00035 FWHFTowerProxyBuilderBase::~FWHFTowerProxyBuilderBase()
00036 {}
00037 
00038 //
00039 // member functions
00040 void
00041 FWHFTowerProxyBuilderBase::setCaloData(const fireworks::Context& ctx)
00042 {
00043    m_vecData  = ctx.getCaloDataHF();// cached to avoid casting
00044    m_caloData = m_vecData;
00045 }
00046 
00047 bool
00048 FWHFTowerProxyBuilderBase::assertCaloDataSlice()
00049 {
00050    if (m_sliceIndex == -1)
00051    {
00052       m_sliceIndex = m_vecData->AddSlice();
00053       // printf("add slice %d \n",m_sliceIndex  );
00054       m_caloData->RefSliceInfo(m_sliceIndex).Setup(item()->name().c_str() , 0.,
00055                                                    item()->defaultDisplayProperties().color(),
00056                                                    item()->defaultDisplayProperties().transparency());
00057     
00058       // add new selector
00059       FWFromTEveCaloDataSelector* sel = 0;
00060       if (m_caloData->GetUserData())
00061       {
00062          FWFromEveSelectorBase* base = reinterpret_cast<FWFromEveSelectorBase*>(m_caloData->GetUserData());
00063          assert(0!=base);
00064          sel = dynamic_cast<FWFromTEveCaloDataSelector*> (base);
00065          assert(0!=sel);
00066       }
00067       else
00068       {
00069          sel = new FWFromTEveCaloDataSelector(m_caloData);
00070          //make sure it is accessible via the base class
00071          m_caloData->SetUserData(static_cast<FWFromEveSelectorBase*>(sel));
00072       }
00073     
00074       sel->addSliceSelector(m_sliceIndex, new FWHFTowerSliceSelector(item(), m_vecData));
00075     
00076       return true;
00077    }
00078    return false;
00079 }
00080 
00081 void
00082 FWHFTowerProxyBuilderBase::build(const FWEventItem* iItem,
00083                                  TEveElementList* el, const FWViewContext* ctx)
00084 {
00085    m_hits=0;
00086    if (iItem)
00087    {
00088       iItem->get(m_hits);
00089       FWCaloDataProxyBuilderBase::build(iItem, el, ctx);
00090    }
00091 }
00092 
00093 void
00094 FWHFTowerProxyBuilderBase::itemBeingDestroyed(const FWEventItem* iItem)
00095 {
00096   
00097    if(0!=m_hits) {
00098 
00099       //reset values for this slice
00100       std::vector<float>& sliceVals = m_vecData->GetSliceVals(m_sliceIndex);
00101       for (std::vector<float>::iterator i = sliceVals.begin(); i!= sliceVals.end(); ++i)
00102       {
00103          *i = 0;
00104       }
00105 
00106 
00107    }
00108    FWCaloDataProxyBuilderBase::itemBeingDestroyed(iItem);
00109 }
00110 
00111 void
00112 FWHFTowerProxyBuilderBase::fillCaloData()
00113 {
00114    //reset values for this slice
00115    std::vector<float>& sliceVals = m_vecData->GetSliceVals(m_sliceIndex);
00116    for (std::vector<float>::iterator i = sliceVals.begin(); i!= sliceVals.end(); ++i)
00117    {
00118       *i = 0;
00119    }
00120 
00121    if (m_hits)
00122    {
00123       TEveCaloData::vCellId_t& selected = m_vecData->GetCellsSelected();
00124 
00125       if(item()->defaultDisplayProperties().isVisible()) {
00126          assert(item()->size() >= m_hits->size());
00127 
00128          unsigned int index=0;
00129          TEveCaloData::vCellId_t cellId;
00130          for(HFRecHitCollection::const_iterator it = m_hits->begin(); it != m_hits->end(); ++it,++index) 
00131          {
00132             const FWEventItem::ModelInfo& info = item()->modelInfo(index);
00133             if(info.displayProperties().isVisible())
00134             {
00135                unsigned int rawid = (*it).detid().rawId();
00136                int tower = fillTowerForDetId(rawid, (*it).energy());
00137                 
00138                if(info.isSelected())
00139                {
00140                   selected.push_back(TEveCaloData::CellId_t(tower, m_sliceIndex));
00141                } 
00142             }
00143          }
00144       }
00145    }
00146 }
00147 
00148 int
00149 FWHFTowerProxyBuilderBase::fillTowerForDetId( unsigned int rawid, float val )
00150 {
00151    using namespace TMath;
00152    const static float upPhiLimit = Pi() -10*DegToRad() -1e-5;
00153 
00154    TEveCaloData::vCellId_t cellIds;
00155    const FWGeometry *geom = item()->getGeom();
00156    if( ! geom->contains( rawid ))
00157    {
00158       fwLog( fwlog::kInfo ) << "FWHFTowerProxyBuilderBase cannot get geometry for DetId: "<< rawid << ". Ignored.\n";
00159       return -1;
00160    }
00161      
00162    const float* corners = geom->getCorners( rawid );
00163    if( ! corners )
00164    {
00165       fwLog( fwlog::kInfo ) << "FWHFTowerProxyBuilderBase cannot get corners for DetId: "<< rawid << ". Ignored.\n";
00166       return -1;
00167    }
00168    
00169    std::vector<TEveVector> front( 4 );
00170    float eta[4], phi[4];
00171    bool plusSignPhi  = false;
00172    bool minusSignPhi = false;
00173    int j = 0;
00174    for( int i = 0; i < 4; ++i )
00175    {     
00176      front[i] = TEveVector( corners[j], corners[j + 1], corners[j + 2] );
00177      j += 3;
00178  
00179      eta[i] = front[i].Eta();
00180      phi[i] = front[i].Phi();
00181 
00182      // make sure sign around Pi is same as sign of fY
00183      phi[i] = Sign( phi[i], front[i].fY );
00184 
00185      ( phi[i] >= 0 ) ? plusSignPhi = true :  minusSignPhi = true;
00186    }
00187 
00188    // check for cell around phi and move up edge to negative side
00189    if( plusSignPhi && minusSignPhi ) 
00190    {
00191       for( int i = 0; i < 4; ++i )
00192       {
00193          if( phi[i] >= upPhiLimit ) 
00194          {
00195             //  printf("over phi max limit %f \n", phi[i]);
00196             phi[i] -= TwoPi();
00197          }
00198       }
00199    }
00200   
00201    float etaM = -10;
00202    float etam =  10;
00203    float phiM = -4;
00204    float phim =  4;
00205    for( int i = 0; i < 4; ++i )
00206    { 
00207       etam = Min( etam, eta[i] );
00208       etaM = Max( etaM, eta[i] );
00209       phim = Min( phim, phi[i] );
00210       phiM = Max( phiM, phi[i] );
00211    }
00212 
00213    /*
00214      if (phiM - phim > 1) 
00215      printf("!!! [%.2f %.2f] input(%.3f, %.3f, %.3f, %.3f) \n", phim, phiM, phiRef[0] , phiRef[1] , phiRef[2],  phiRef[3]);
00216    */
00217 
00218    // check if tower is there
00219    Float_t ceta = (etam+etaM)*0.5;
00220    Float_t cphi = (phim+phiM)*0.5;
00221    int tower = -1;
00222    int idx = 0;
00223    for ( TEveCaloData::vCellGeom_i i = m_vecData->GetCellGeom().begin(); i!= m_vecData->GetCellGeom().end(); ++i, ++idx)
00224    {
00225       const TEveCaloData::CellGeom_t &cg = *i;
00226       if ((ceta > cg.fEtaMin && ceta < cg.fEtaMax) && (cphi > cg.fPhiMin && cphi < cg.fPhiMax))
00227       {
00228          tower = idx;
00229          break;
00230       }
00231    }
00232 
00233    // add it if not there 
00234    if (tower == -1 )
00235    {
00236       tower = m_vecData->AddTower(etam, etaM, phim, phiM);
00237    }
00238 
00239 
00240    m_vecData->FillSlice(m_sliceIndex, tower, val);
00241    return tower; 
00242 }
00243 
00244 REGISTER_FWPROXYBUILDER(FWHFTowerProxyBuilderBase, HFRecHitCollection, "HFLego", FWViewType::kLegoHFBit |FWViewType::kAllRPZBits | FWViewType::k3DBit );
00245 
00246