CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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.21 2011/02/23 11:34:52 amraktad 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    
00164    std::vector<TEveVector> front( 4 );
00165    float eta[4], phi[4];
00166    bool plusSignPhi  = false;
00167    bool minusSignPhi = false;
00168    int j = 0;
00169    for( int i = 0; i < 4; ++i )
00170    {     
00171      front[i] = TEveVector( corners[j], corners[j + 1], corners[j + 2] );
00172      j += 3;
00173  
00174      eta[i] = front[i].Eta();
00175      phi[i] = front[i].Phi();
00176 
00177      // make sure sign around Pi is same as sign of fY
00178      phi[i] = Sign( phi[i], front[i].fY );
00179 
00180      ( phi[i] >= 0 ) ? plusSignPhi = true :  minusSignPhi = true;
00181    }
00182 
00183    // check for cell around phi and move up edge to negative side
00184    if( plusSignPhi && minusSignPhi ) 
00185    {
00186       for( int i = 0; i < 4; ++i )
00187       {
00188          if( phi[i] >= upPhiLimit ) 
00189          {
00190             //  printf("over phi max limit %f \n", phi[i]);
00191             phi[i] -= TwoPi();
00192          }
00193       }
00194    }
00195   
00196    float etaM = -10;
00197    float etam =  10;
00198    float phiM = -4;
00199    float phim =  4;
00200    for( int i = 0; i < 4; ++i )
00201    { 
00202       etam = Min( etam, eta[i] );
00203       etaM = Max( etaM, eta[i] );
00204       phim = Min( phim, phi[i] );
00205       phiM = Max( phiM, phi[i] );
00206    }
00207 
00208    /*
00209      if (phiM - phim > 1) 
00210      printf("!!! [%.2f %.2f] input(%.3f, %.3f, %.3f, %.3f) \n", phim, phiM, phiRef[0] , phiRef[1] , phiRef[2],  phiRef[3]);
00211    */
00212 
00213    // check if tower is there
00214    Float_t ceta = (etam+etaM)*0.5;
00215    Float_t cphi = (phim+phiM)*0.5;
00216    int tower = -1;
00217    int idx = 0;
00218    for ( TEveCaloData::vCellGeom_i i = m_vecData->GetCellGeom().begin(); i!= m_vecData->GetCellGeom().end(); ++i, ++idx)
00219    {
00220       const TEveCaloData::CellGeom_t &cg = *i;
00221       if ((ceta > cg.fEtaMin && ceta < cg.fEtaMax) && (cphi > cg.fPhiMin && cphi < cg.fPhiMax))
00222       {
00223          tower = idx;
00224          break;
00225       }
00226    }
00227 
00228    // add it if not there 
00229    if (tower == -1 )
00230    {
00231       tower = m_vecData->AddTower(etam, etaM, phim, phiM);
00232    }
00233 
00234 
00235    m_vecData->FillSlice(m_sliceIndex, tower, val);
00236    return tower; 
00237 }
00238 
00239 REGISTER_FWPROXYBUILDER(FWHFTowerProxyBuilderBase, HFRecHitCollection, "HFLego", FWViewType::kLegoHFBit |FWViewType::kAllRPZBits | FWViewType::k3DBit );
00240 
00241