CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/Fireworks/Core/src/FWRPZViewGeometry.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWRPZViewGeometry
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  Alja Mrak-Tadel
00010 //         Created:  Thu Mar 25 20:33:06 CET 2010
00011 // $Id: FWRPZViewGeometry.cc,v 1.19 2011/10/15 02:33:30 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <iostream>
00016 #include <cassert>
00017 
00018 // user include files
00019 #include "TGeoBBox.h"
00020 
00021 #include "TEveElement.h"
00022 #include "TEveCompound.h"
00023 #include "TEveScene.h"
00024 #include "TEvePointSet.h"
00025 #include "TEveStraightLineSet.h"
00026 #include "TEveGeoNode.h"
00027 #include "TEveManager.h"
00028 #include "TEveProjectionManager.h"
00029 
00030 #include "Fireworks/Core/interface/FWRPZViewGeometry.h"
00031 #include "Fireworks/Core/interface/FWGeometry.h"
00032 #include "Fireworks/Core/interface/FWColorManager.h"
00033 #include "Fireworks/Core/interface/Context.h"
00034 #include "Fireworks/Core/interface/fwLog.h"
00035 
00036 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
00037 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00038 
00039 //
00040 //
00041 //
00042 // constants, enums and typedefs
00043 //
00044 
00045 //
00046 // static data member definitions
00047 //
00048 
00049 //
00050 // constructors and destructor
00051 //
00052 FWRPZViewGeometry::FWRPZViewGeometry(const fireworks::Context& context):
00053    FWViewGeometryList(context),
00054 
00055    m_rhoPhiGeo(0),
00056    m_rhoZGeo(0),
00057 
00058    m_pixelBarrelElements(0),
00059    m_pixelEndcapElements(0),
00060    m_trackerBarrelElements(0),
00061    m_trackerEndcapElements(0)
00062 {
00063    SetElementName("RPZGeomShared");
00064 }
00065 
00066 // FWRPZViewGeometry::FWRPZViewGeometry(const FWRPZViewGeometry& rhs)
00067 // {
00068 //    // do actual copying here;
00069 // }
00070 
00071 FWRPZViewGeometry::~FWRPZViewGeometry()
00072 {
00073    m_rhoPhiGeo->DecDenyDestroy();
00074    m_rhoZGeo->DecDenyDestroy();
00075 }
00076 
00077 //______________________________________________________________________________
00078 
00079 void
00080 FWRPZViewGeometry::initStdGeoElements(const FWViewType::EType type)
00081 {
00082    assert(m_geom != 0);
00083 
00084    if (type == FWViewType::kRhoZ)
00085    {
00086       AddElement(makeMuonGeometryRhoZ());
00087       AddElement(makeCaloOutlineRhoZ());
00088    }
00089    else 
00090    {
00091       AddElement(makeMuonGeometryRhoPhi());
00092       AddElement(makeCaloOutlineRhoPhi());
00093    }
00094 }
00095 
00096 //______________________________________________________________________________
00097 
00098 
00099 TEveElement*
00100 FWRPZViewGeometry::makeCaloOutlineRhoZ()
00101 {
00102    using namespace fireworks;
00103 
00104    float ri = m_context.caloZ2()*tan(2*atan(exp(-m_context.caloMaxEta())));
00105 
00106    TEveStraightLineSet* el = new TEveStraightLineSet( "TrackerRhoZoutline" );
00107    el->SetPickable(kFALSE);
00108    addToCompound(el, kFWTrackerBarrelColorIndex, false);
00109 
00110    el->AddLine(0,  m_context.caloR1(), -m_context.caloZ1(), 0,  m_context.caloR1(),  m_context.caloZ1());
00111    el->AddLine(0, -m_context.caloR1(),  m_context.caloZ1(), 0, -m_context.caloR1(), -m_context.caloZ1());
00112 
00113    el->AddLine(0, -m_context.caloR2(),   m_context.caloZ2(), 0,  -ri,   m_context.caloZ2());
00114    el->AddLine(0, ri,  m_context.caloZ2(), 0,  m_context.caloR2(), m_context.caloZ2());
00115 
00116    el->AddLine(0, -m_context.caloR2(),   -m_context.caloZ2(), 0,  -ri,   -m_context.caloZ2());
00117    el->AddLine(0, ri,  -m_context.caloZ2(), 0,  m_context.caloR2(), -m_context.caloZ2());
00118  
00119    return el;
00120 }
00121 
00122 TEveElement*
00123 FWRPZViewGeometry::makeCaloOutlineRhoPhi()
00124 { 
00125    TEveStraightLineSet* el = new TEveStraightLineSet( "TrackerRhoPhi" );
00126    addToCompound(el, kFWTrackerBarrelColorIndex, false);
00127 
00128    el->SetLineColor(m_context.colorManager()->geomColor(kFWTrackerBarrelColorIndex));
00129    const unsigned int nSegments = 100;
00130    const double r =  m_context.caloR1();
00131    for ( unsigned int i = 1; i <= nSegments; ++i )
00132       el->AddLine(r*sin(TMath::TwoPi()/nSegments*(i-1)), r*cos(TMath::TwoPi()/nSegments*(i-1)), 0,
00133                   r*sin(TMath::TwoPi()/nSegments*i), r*cos(TMath::TwoPi()/nSegments*i), 0);
00134 
00135    TEvePointSet* ref = new TEvePointSet("reference");
00136    ref->SetTitle("(0,0,0)");
00137    ref->SetMarkerStyle(4);
00138    ref->SetMarkerColor(kWhite);
00139    ref->SetNextPoint(0.,0.,0.);
00140    el->AddElement(ref);
00141 
00142    return el;
00143 }
00144 
00145 //______________________________________________________________________________
00146 
00147 TEveElement*
00148 FWRPZViewGeometry::makeMuonGeometryRhoPhi( void )
00149 {
00150    Int_t iWheel = 0;
00151  
00152    // rho-phi view
00153    TEveCompound* container = new TEveCompound( "MuonRhoPhi" );
00154 
00155 
00156    for( Int_t iStation = 1; iStation <= 4; ++iStation )
00157    {
00158       for( Int_t iSector = 1 ; iSector <= 14; ++iSector )
00159       {
00160          if( iStation < 4 && iSector > 12 ) continue;
00161          DTChamberId id( iWheel, iStation, iSector );
00162          TEveGeoShape* shape = m_geom->getEveShape( id.rawId() );
00163          if( shape ) 
00164          {
00165             shape->SetMainColor(m_colorComp[kFWMuonBarrelLineColorIndex]->GetMainColor());
00166             addToCompound(shape, kFWMuonBarrelLineColorIndex);
00167             container->AddElement( shape );
00168          }
00169       }
00170    }
00171    return container;
00172 }
00173 
00174 //______________________________________________________________________________
00175 
00176 TEveElement*
00177 FWRPZViewGeometry::makeMuonGeometryRhoZ( void )
00178 {
00179    TEveElementList* container = new TEveElementList( "MuonRhoZ" );
00180 
00181    {
00182       TEveCompound* dtContainer = new TEveCompound( "DT" );
00183       for( Int_t iWheel = -2; iWheel <= 2; ++iWheel )
00184       {
00185          for( Int_t iStation = 1; iStation <= 4; ++iStation )
00186          {
00187             float min_rho(1000), max_rho(0), min_z(2000), max_z(-2000);
00188 
00189             // This will give us a quarter of DTs
00190             // which is enough for our projection
00191             for( Int_t iSector = 1; iSector <= 4; ++iSector )
00192             {
00193                DTChamberId id( iWheel, iStation, iSector );
00194                unsigned int rawid = id.rawId();
00195                FWGeometry::IdToInfoItr det = m_geom->find( rawid );
00196                estimateProjectionSizeDT( *det, min_rho, max_rho, min_z, max_z );
00197             }
00198             if ( min_rho > max_rho || min_z > max_z ) continue;
00199             TEveElement* se =  makeShape( min_rho, max_rho, min_z, max_z );
00200             addToCompound(se, kFWMuonBarrelLineColorIndex);
00201             dtContainer->AddElement(se);
00202             se =  makeShape( -max_rho, -min_rho, min_z, max_z );
00203             addToCompound(se, kFWMuonBarrelLineColorIndex);
00204             dtContainer->AddElement(se);
00205          }
00206       }
00207 
00208       container->AddElement( dtContainer );
00209    }
00210    {
00211       TEveCompound* cscContainer = new TEveCompound( "CSC" );
00212    
00213       Int_t maxChambers = 36;
00214       Int_t step = 9;
00215       Int_t iLayer = 0; // chamber
00216       for( Int_t iEndcap = 1; iEndcap <= 2; ++iEndcap ) // 1=forward (+Z), 2=backward(-Z)
00217       {
00218          // Actual CSC geometry:
00219          // Station 1 has 4 rings with 36 chambers in each
00220          // Station 2: ring 1 has 18 chambers, ring 2 has 36 chambers
00221          // Station 3: ring 1 has 18 chambers, ring 2 has 36 chambers
00222          // Station 4: ring 1 has 18 chambers
00223          for( Int_t iStation = 1; iStation <= 4; ++iStation )
00224          {
00225             for( Int_t iRing = 1; iRing <= 4; ++iRing )
00226             {
00227                if( iStation > 1 && iRing > 2 ) continue;
00228                if( iStation > 3 && iRing > 1 ) continue;
00229                float min_rho(1000), max_rho(0), min_z(2000), max_z(-2000);
00230                ( iRing == 1 && iStation > 1 ) ? ( maxChambers = 18 ) : ( maxChambers = 36 );
00231                ( iRing == 1 && iStation > 1 ) ? ( step = 5 ) : (  step = 18 );
00232             
00233                // Skip most of the chambers since they will project
00234                // the same way as the two top ones and the two bottom ones
00235                for( Int_t iChamber = step; iChamber <= maxChambers; iChamber += step )
00236                {
00237                   CSCDetId id( iEndcap, iStation, iRing, iChamber, iLayer );
00238                   FWGeometry::IdToInfoItr det = m_geom->find( id.rawId() );
00239                   estimateProjectionSizeCSC( *det, min_rho, max_rho, min_z, max_z );
00240 
00241                   // and a chamber next to it
00242                   ++iChamber;
00243                   CSCDetId nextid( iEndcap, iStation, iRing, iChamber, iLayer );
00244                   det = m_geom->find( nextid.rawId() );
00245                   estimateProjectionSizeCSC( *det, min_rho, max_rho, min_z, max_z );
00246                }
00247                if ( min_rho > max_rho || min_z > max_z ) continue;
00248 
00249                TEveElement* se = makeShape( min_rho, max_rho, min_z, max_z);
00250                addToCompound(se, kFWMuonEndcapLineColorIndex);
00251                cscContainer->AddElement(se);
00252 
00253                se = makeShape( -max_rho, -min_rho, min_z, max_z );
00254                addToCompound(se, kFWMuonEndcapLineColorIndex);
00255                cscContainer->AddElement(se);
00256             }
00257          }
00258       }
00259       container->AddElement( cscContainer );
00260    }
00261    return container;
00262 }
00263 
00264 //______________________________________________________________________________
00265 
00266 TEveGeoShape*
00267 FWRPZViewGeometry::makeShape( double min_rho, double max_rho, double min_z, double max_z)
00268 {
00269    TEveTrans t;
00270    t(1,1) = 1; t(1,2) = 0; t(1,3) = 0;
00271    t(2,1) = 0; t(2,2) = 1; t(2,3) = 0;
00272    t(3,1) = 0; t(3,2) = 0; t(3,3) = 1;
00273    t(1,4) = 0; t(2,4) = (min_rho+max_rho)/2; t(3,4) = (min_z+max_z)/2;
00274 
00275    TEveGeoShape* shape = new TEveGeoShape;
00276    shape->SetTransMatrix(t.Array());
00277 
00278    shape->SetRnrSelf(kTRUE);
00279    shape->SetRnrChildren(kTRUE);
00280    TGeoBBox* box = new TGeoBBox( 0, (max_rho-min_rho)/2, (max_z-min_z)/2 );
00281    shape->SetShape( box );
00282 
00283    return shape;
00284 }
00285 
00286 //______________________________________________________________________________
00287 
00288 void
00289 FWRPZViewGeometry::estimateProjectionSizeDT( const FWGeometry::GeomDetInfo& info,
00290                                              float& min_rho, float& max_rho, float& min_z, float& max_z )
00291 {
00292    // we will test 5 points on both sides ( +/- z)
00293    float local[3], global[3];
00294 
00295    float dX = info.shape[1];
00296    float dY = info.shape[2];
00297    float dZ = info.shape[3];
00298 
00299    local[0] = 0; local[1] = 0; local[2] = dZ;
00300    m_geom->localToGlobal( info, local, global );
00301    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00302 
00303    local[0] = dX; local[1] = dY; local[2] = dZ;
00304    m_geom->localToGlobal( info, local, global );
00305    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00306 
00307    local[0] = -dX; local[1] = dY; local[2] = dZ;
00308    m_geom->localToGlobal( info, local, global );
00309    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00310 
00311    local[0] = dX; local[1] = -dY; local[2] = dZ;
00312    m_geom->localToGlobal( info, local, global );
00313    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00314 
00315    local[0] = -dX; local[1] = -dY; local[2] = dZ;
00316    m_geom->localToGlobal( info, local, global );
00317    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00318 
00319    local[0] = 0; local[1] = 0; local[2] = -dZ;
00320    m_geom->localToGlobal( info, local, global );
00321    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00322 
00323    local[0] = dX; local[1] = dY; local[2] = -dZ;
00324    m_geom->localToGlobal( info, local, global );
00325    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00326 
00327    local[0] = -dX; local[1] = dY; local[2] = -dZ;
00328    m_geom->localToGlobal( info, local, global );
00329    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00330 
00331    local[0] = dX; local[1] = -dY; local[2] = -dZ;
00332    m_geom->localToGlobal( info, local, global );
00333    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00334 
00335    local[0] = -dX; local[1] = -dY; local[2] = -dZ;
00336    m_geom->localToGlobal( info, local, global );
00337    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00338 }
00339 
00340 void
00341 FWRPZViewGeometry::estimateProjectionSizeCSC( const FWGeometry::GeomDetInfo& info,
00342                                               float& min_rho, float& max_rho, float& min_z, float& max_z )
00343 {
00344    float local[3], global[3];
00345 
00346    float dX = info.shape[2] - info.shape[1];
00347    float dY = info.shape[4];
00348    float ddY = sqrt( 4 * dY * dY + dX * dX ) * 0.5;
00349    float dZ = info.shape[3];
00350    
00351    local[0] = info.shape[2]; local[1] = ddY; local[2] = -dZ;
00352    m_geom->localToGlobal( info, local, global );
00353    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00354 
00355    local[0] = info.shape[1]; local[1] = -ddY; local[2] = -dZ;
00356    m_geom->localToGlobal( info, local, global );
00357    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00358 
00359    local[0] = info.shape[1]; local[1] = -ddY; local[2] = dZ;
00360    m_geom->localToGlobal( info, local, global );
00361    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00362 
00363    local[0] = info.shape[2]; local[1] = ddY; local[2] = dZ;
00364    m_geom->localToGlobal( info, local, global );
00365    estimateProjectionSize( global, min_rho, max_rho, min_z, max_z );
00366 }
00367 
00368 void
00369 FWRPZViewGeometry::estimateProjectionSize( const float* global,
00370                                            float& min_rho, float& max_rho, float& min_z, float& max_z )
00371 {
00372    double rho = sqrt(global[0] *global[0]+global[1] *global[1]);
00373    if ( min_rho > rho ) min_rho = rho;
00374    if ( max_rho < rho ) max_rho = rho;
00375    if ( min_z > global[2] ) min_z = global[2];
00376    if ( max_z < global[2] ) max_z = global[2];
00377 }
00378 
00379 
00380 // ATODO:: check white vertex -> shouldn't be relative to background
00381 //         when detruction ?
00382 
00383 
00384 // ATODO why color is not set in 3D original, why cast to polygonsetprojected after projected ????
00385 // is geom color dynamic --- independent of projection manager
00386 
00387 // NOTE geomtry MuonRhoZAdanced renamed to  MuonRhoZ
00388 
00389 
00390 //==============================================================================
00391 //==============================================================================
00392 
00393 
00394 
00395 void
00396 FWRPZViewGeometry::showPixelBarrel( bool show )
00397 {
00398    if( !m_pixelBarrelElements && show )
00399    { 
00400       m_pixelBarrelElements = new TEveElementList("PixelBarrel");
00401       AddElement(m_pixelBarrelElements);
00402       std::vector<unsigned int> ids = m_geom->getMatchedIds( FWGeometry::Tracker, FWGeometry::PixelBarrel );
00403       for( std::vector<unsigned int>::const_iterator id = ids.begin();
00404            id != ids.end(); ++id )
00405       {
00406          TEveGeoShape* shape = m_geom->getEveShape( *id );
00407          shape->SetTitle(Form("PixelBarrel %d",*id));
00408          addToCompound(shape, kFWPixelBarrelColorIndex);
00409          m_pixelBarrelElements->AddElement( shape );
00410       }
00411       importNew(m_pixelBarrelElements);
00412    }
00413 
00414    if (m_pixelBarrelElements)
00415    {
00416       m_pixelBarrelElements->SetRnrState(show);
00417       gEve->Redraw3D();
00418    }
00419 }
00420 
00421 void
00422 FWRPZViewGeometry::showPixelEndcap( bool show )
00423 {
00424    if( !m_pixelEndcapElements && show )
00425    {
00426       m_pixelEndcapElements = new TEveElementList("PixelEndcap");
00427 
00428       std::vector<unsigned int> ids = m_geom->getMatchedIds( FWGeometry::Tracker, FWGeometry::PixelEndcap );
00429       for( std::vector<unsigned int>::const_iterator id = ids.begin();
00430            id != ids.end(); ++id )
00431       {
00432          TEveGeoShape* shape = m_geom->getEveShape( *id );
00433 
00434          shape->SetTitle(Form("PixelEndCap %d",*id));
00435          addToCompound(shape, kFWPixelEndcapColorIndex);
00436          m_pixelEndcapElements->AddElement( shape );
00437       }
00438 
00439       AddElement(m_pixelEndcapElements);
00440       importNew(m_pixelEndcapElements);
00441    }
00442 
00443    if (m_pixelEndcapElements)
00444    {
00445       m_pixelEndcapElements->SetRnrState(show);
00446       gEve->Redraw3D();
00447    }
00448 }
00449 
00450 
00451 void
00452 FWRPZViewGeometry::showTrackerBarrel( bool show )
00453 {
00454    if( !m_trackerBarrelElements && show )
00455    {
00456       m_trackerBarrelElements = new TEveElementList("TrackerBarrel");
00457 
00458       std::vector<unsigned int> ids = m_geom->getMatchedIds( FWGeometry::Tracker, FWGeometry::TIB );
00459       for( std::vector<unsigned int>::const_iterator id = ids.begin();
00460            id != ids.end(); ++id )
00461       {
00462          TEveGeoShape* shape = m_geom->getEveShape( *id ); 
00463          addToCompound(shape, kFWTrackerBarrelColorIndex);
00464          m_trackerBarrelElements->AddElement( shape );
00465       }
00466       ids = m_geom->getMatchedIds( FWGeometry::Tracker, FWGeometry::TOB );
00467       for( std::vector<unsigned int>::const_iterator id = ids.begin();
00468            id != ids.end(); ++id )
00469       {
00470          TEveGeoShape* shape = m_geom->getEveShape( *id );
00471 
00472          shape->SetTitle(Form("TrackerBarrel %d",*id));
00473          addToCompound(shape, kFWTrackerBarrelColorIndex);
00474          m_trackerBarrelElements->AddElement( shape );
00475       }
00476 
00477       AddElement(m_trackerBarrelElements);
00478       importNew(m_trackerBarrelElements);
00479    }
00480 
00481    if (m_trackerBarrelElements)
00482    {
00483       m_trackerBarrelElements->SetRnrState(show);
00484       gEve->Redraw3D();
00485    }
00486 }
00487 
00488 void
00489 FWRPZViewGeometry::showTrackerEndcap( bool show )
00490 {
00491    if( !m_trackerEndcapElements && show )
00492    {
00493       m_trackerEndcapElements = new TEveElementList("TrackerEndcap");
00494 
00495       std::vector<unsigned int> ids = m_geom->getMatchedIds( FWGeometry::Tracker, FWGeometry::TID );
00496       for( std::vector<unsigned int>::const_iterator id = ids.begin();
00497            id != ids.end(); ++id )
00498       {
00499          TEveGeoShape* shape = m_geom->getEveShape( *id );
00500          addToCompound(shape, kFWTrackerEndcapColorIndex);
00501          m_trackerEndcapElements->AddElement( shape );
00502       }
00503       ids = m_geom->getMatchedIds( FWGeometry::Tracker, FWGeometry::TEC );
00504       for( std::vector<unsigned int>::const_iterator id = ids.begin();
00505            id != ids.end(); ++id )
00506       {
00507          TEveGeoShape* shape = m_geom->getEveShape( *id );
00508 
00509          shape->SetTitle(Form("TrackerEndcap %d",*id));
00510          addToCompound(shape, kFWTrackerEndcapColorIndex);
00511          m_trackerEndcapElements->AddElement( shape );
00512       }
00513 
00514       AddElement(m_trackerEndcapElements);
00515       importNew(m_trackerEndcapElements);
00516    }
00517 
00518    if (m_trackerEndcapElements)
00519    {
00520       m_trackerEndcapElements->SetRnrState(show);
00521       gEve->Redraw3D();
00522    }
00523 }
00524 
00525 
00526 void FWRPZViewGeometry::importNew(TEveElementList* x)
00527 {
00528    TEveProjected* proj =  *BeginProjecteds();
00529    proj->GetManager()->SubImportElements(x,  proj->GetProjectedAsElement());
00530                                
00531 }