CMS 3D CMS Logo

VisPSimHitTwig.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
00002 
00003 #include "VisSimulation/VisSimHits/interface/VisPSimHitTwig.h"
00004 #include "VisFramework/VisFrameworkBase/interface/VisTwigFactroyService.h"
00005 #include "VisFramework/VisFrameworkBase/interface/VisEventSelector.h"
00006 #include "VisFramework/VisFrameworkBase/interface/debug.h"
00007 #include "VisFramework/VisEventSetup/interface/VisEventSetupService.h"
00008 #include "DataFormats/DetId/interface/DetId.h"
00009 #include "FWCore/Framework/interface/Event.h"
00010 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00011 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00012 #include "Iguana/Framework/interface/IgRepSet.h"
00013 #include "Iguana/Studio/interface/IgDocumentData.h"
00014 #include "Iguana/Studio/interface/IgQtLock.h"
00015 #include "Iguana/Models/interface/IgTextRep.h"
00016 #include "Iguana/GLModels/interface/Ig3DBaseModel.h"
00017 #include "Iguana/GLModels/interface/Ig3DRep.h"
00018 #include "Iguana/GLModels/interface/IgLegoRep.h"
00019 #include "Iguana/GLModels/interface/IgRPhiRep.h"
00020 #include "Iguana/GLModels/interface/IgRZRep.h"
00021 #include <Inventor/nodes/SoSeparator.h>
00022 #include <Inventor/nodes/SoDrawStyle.h>
00023 #include <Inventor/nodes/SoMaterial.h>
00024 #include <Inventor/nodes/SoLineSet.h>
00025 #include <Inventor/nodes/SoPointSet.h>
00026 #include <Inventor/nodes/SoVertexProperty.h>
00027 #include <qstring.h>
00028 #include <iostream>
00029 #include <iomanip>
00030 #include <stdexcept>
00031 
00032 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00033 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00034 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00035 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00036 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00037 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00038 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00039 
00040 namespace 
00041 {
00042     VisQueuedTwig *
00043     createThisTwig (IgState *state, IgTwig *parent,
00044                     const std::string &name,
00045                     const std::string &friendlyName,
00046                     const std::string &modLabel,
00047                     const std::string &instanceName,
00048                     const std::string &processName)
00049     {
00050         IgTwig *rootTwig = IgDocumentData::get (state)->root ();
00051         IgTwig *eventTwig = 0;
00052         eventTwig = rootTwig->lookup ("/Objects/CMS Event and Detector/Simulated Data/Sim Hits");
00053         
00054         if (! eventTwig) 
00055             eventTwig = parent;
00056                 
00057         return new VisPSimHitTwig (state, eventTwig, "[N/A] PSimHit (" + name + ")", friendlyName, modLabel, instanceName, processName);
00058     }
00059 }
00060 
00061 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00062 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
00063 
00064 VisPSimHitTwig::VisPSimHitTwig (IgState *state, IgTwig *parent, const std::string &name /* = ""*/,
00065                                 const std::string &friendlyName /* = ""*/,
00066                                 const std::string &moduleLabel /* = ""*/,
00067                                 const std::string &instanceName /* = ""*/,
00068                                 const std::string &processName /* = "" */)
00069     : VisQueuedTwig (state, parent, name),
00070       m_text (name),
00071       m_friendlyName (friendlyName),
00072       m_moduleLabel (moduleLabel),
00073       m_instanceName (instanceName),
00074       m_processName (processName),
00075       m_connect (state, lat::CreateCallback (this, &VisPSimHitTwig::twigChanged))
00076 {
00077     VisTwigFactroyService *tfService = VisTwigFactroyService::get (state);
00078     if (! tfService)
00079     {
00080         tfService = new VisTwigFactroyService (state);
00081     }   
00082     edm::TypeID caloJetsID (typeid (edm::PSimHitContainer));
00083     tfService->registerTwig (caloJetsID.friendlyClassName (), &createThisTwig);
00084 }
00085 
00086 void
00087 VisPSimHitTwig::twigChanged (void)
00088 { IgRepSet::invalidate (this, SELF_MASK); }
00089 
00090 void
00091 VisPSimHitTwig::onNewEvent (const edm::Event &event,
00092                             const edm::EventSetup &eventSetup)
00093 {
00094     // Get debugging dump.
00095     VisQueuedTwig::onNewEvent (event, eventSetup);
00096 
00097     m_hits.clear ();
00098     m_text = (QString ("Run %1, Event %2, LS %3, Orbit %4, BX %5")
00099               .arg (event.id ().run ())
00100               .arg (event.id ().event ())
00101               .arg (event.luminosityBlock ())
00102               .arg (event.orbitNumber ())
00103               .arg (event.bunchCrossing ())
00104               .latin1 ());
00105 
00106     std::vector<edm::Handle<edm::PSimHitContainer> > hitCollections;
00107     
00108     try
00109     {
00110         if ((! m_friendlyName.empty ()) || (! m_moduleLabel.empty ()) || (! m_instanceName.empty ()) || (! m_processName.empty ()))
00111         {
00112             VisEventSelector visSel (m_friendlyName, m_moduleLabel, m_instanceName, m_processName);
00113             event.getMany (visSel, hitCollections);
00114         }
00115     }
00116     catch (cms::Exception& e)
00117     {
00118         e.append (" from VisPSimHitTwig::onNewEvent ");
00119         e.append (this->name ());    
00120 
00121         if (this->m_onCmsException)
00122             this->m_onCmsException (&e);
00123     }
00124     catch (lat::Error &e) 
00125     {
00126         if (this->m_onError)
00127             this->m_onError (&e);
00128     }
00129     catch (std::exception &e) 
00130     {
00131         if (this->m_onException)
00132             this->m_onException (&e);
00133     }
00134     catch (...) 
00135     {
00136         if (this->m_onUnhandledException)
00137             this->m_onUnhandledException ();
00138     }
00139 
00140     if (! hitCollections.empty ())
00141     {
00142         for (std::vector<edm::Handle<edm::PSimHitContainer> >::iterator i = hitCollections.begin (), iEnd = hitCollections.end (); i != iEnd; ++i) 
00143         {
00144             const edm::PSimHitContainer& c = *(*i);
00145             QString sizeStr = (QString ("%1").arg (c.size ()));
00146             QString nameStr = QString (this->name ());
00147             int ib = nameStr.find ("[");
00148             int ie = nameStr.find ("]");
00149             nameStr.replace (ib + 1, ie - 1, sizeStr);
00150             
00151             this->name (nameStr);
00152 
00153             for (std::vector<PSimHit>::const_iterator isim = c.begin (), isimEnd = c.end ();
00154                  isim != isimEnd; ++isim)
00155             {
00156                 m_hits.push_back ((*isim));
00157             }    
00158         }
00159     }
00160 
00161     IgRepSet::invalidate (this, IgTwig::SELF_MASK | IgTwig::STRUCTURE_MASK);
00162 }
00163 
00164 void
00165 VisPSimHitTwig::update (IgTextRep *rep)
00166 {
00167     // Get debugging dump.
00168     VisQueuedTwig::update (rep);
00169 
00170     // Prepare property description.
00171     std::ostringstream  text;
00172     text << "Total " << m_hits.size () << " hits from ";
00173     text << m_text << "<br>";
00174 
00175     text << "<br>";
00176 
00177     text << "<table width='100%' border=1>"
00178          << "<TR align = center>"
00179          << "<TH>Number</TH>"
00180          << "<TH>Position</TH>"
00181          << "<TH>Energy loss</TH>"
00182          << "<TH>DetID</TH>"
00183          << "<TH>Track ID</TH>"
00184          << "<TH>Time of flight</TH>"
00185          << "<TH>Particle type</TH>"
00186          << "<TH>Process type</TH>"
00187          << "</TR>";
00188     text << setiosflags (std::ios::showpoint | std::ios::fixed);
00189     text.setf (std::ios::right, std::ios::adjustfield);
00190 
00191     if ((! m_hits.empty ())) 
00192     {
00193         int nHits = 0;
00194         try 
00195         {
00196             for (std::vector<PSimHit>::const_iterator i = m_hits.begin (); i != m_hits.end (); ++i) 
00197             {
00198                 text << "<TR align = right>"
00199                      << "<TD>" << std::setw (3) << nHits++ << "</TD>"
00200                      << "<TD>" << (*i).localPosition ().x () << ", " 
00201                                << (*i).localPosition ().y () << ", " 
00202                      << (*i).localPosition ().z () << "; Phi "<< (*i).localPosition ().phi ()  << "</TD>"
00203                      << "<TD>" << std::setw (6) << std::setprecision (3) << (*i).energyLoss () << "</TD>"
00204                      << "<TD>" << std::setw (6) << std::setprecision (3) << (*i).detUnitId () << "</TD>"
00205                      << "<TD>" << std::setw (6) << std::setprecision (3) << (*i).trackId () << "</TD>"
00206                      << "<TD>" << std::setw (6) << std::setprecision (3) << (*i).timeOfFlight () << "</TD>"
00207                      << "<TD>" << std::setw (6) << std::setprecision (3) << (*i).particleType () << "</TD>"
00208                      << "<TD>" << std::setw (6) << std::setprecision (3) << (*i).processType () << "</TD></TR>";
00209             }
00210         }
00211         catch (cms::Exception& e)
00212         {
00213             if (this->m_onCmsException)
00214                 this->m_onCmsException (&e);
00215         }
00216         catch (lat::Error &e) 
00217         {
00218             if (this->m_onError)
00219                 this->m_onError (&e);
00220         }
00221         catch (std::exception &e) 
00222         {
00223             if (this->m_onException)
00224                 this->m_onException (&e);
00225         }
00226         catch (...) 
00227         {
00228             if (this->m_onUnhandledException)
00229                 this->m_onUnhandledException ();
00230         }
00231         text << "</table>";
00232     }
00233 
00234     // Send it over.
00235     IgQtLock ();
00236     rep->setText (text.str ());
00237 }
00238 
00239 void
00240 VisPSimHitTwig::update (Ig3DRep *rep)
00241 {
00242     // Get debugging dump.
00243     VisQueuedTwig::update (rep);      
00244     IgQtLock ();
00245     rep->clear ();
00246 
00247     if (! m_hits.empty ())
00248     {
00249         SoMaterial *mat = new SoMaterial;
00250         mat->diffuseColor.setValue (0.0, 0.0, 1.0);
00251         mat->emissiveColor.setValue(0.0, 0.0, 1.0);
00252 
00253         SoDrawStyle *drawStyle = new SoDrawStyle;
00254         drawStyle->pointSize = 6.0;
00255 
00256         SoSeparator *sep = new SoSeparator; // global
00257         sep->addChild (mat);
00258         sep->addChild (drawStyle);
00259 
00260         VisEventSetupService *esService = VisEventSetupService::get (state ());
00261         ASSERT (esService);
00262 
00263         try 
00264         {
00265             typedef std::map<int, std::vector <SbVec3f> > VisParticleMap;
00266             VisParticleMap pmap;
00267 
00268             for (std::vector<PSimHit>::const_iterator i = m_hits.begin (); i != m_hits.end (); ++i) 
00269             {
00270                 int ptype = (*i).trackId ();
00271                 
00272                 VisParticleMap::iterator mit = pmap.find (ptype);
00273                 
00274                 if (mit == pmap.end ())
00275                 {
00276                     std::vector<SbVec3f> vec;
00277                     bool done = pmap.insert (VisParticleMap::value_type (ptype, vec)).second;
00278                     if (done)
00279                     {
00280                         mit = pmap.find (ptype);
00281                     }
00282                 }
00283 
00284                 if (mit == pmap.end ())
00285                 {
00286                     throw std::runtime_error ("Cannot find particle type in the map!");
00287                 }               
00288                 DetId id = (*i).detUnitId ();
00289                 if (! id.null () && (id.det () == DetId::Tracker || id.det () == DetId::Muon))
00290                 {
00291                     GlobalPoint pos = esService->toGlobal ((*i).localPosition (), id);
00292                     (mit->second).push_back (SbVec3f (pos.x () / 100.0, pos.y () / 100.0, pos.z () / 100.0));
00293                 }
00294             }       
00295             if (! pmap.empty ())
00296             {
00297                 for (VisParticleMap::const_iterator mit = pmap.begin (); mit != pmap.end (); ++mit)
00298                 {
00299                     SoSeparator *trackSep = new SoSeparator; 
00300                     
00301                     QString label = QString ("ID = %1")
00302                                     .arg (mit->first);             
00303 
00304                     trackSep->setName (Ig3DBaseModel::encode (label.prepend ("Track with ")));
00305 
00306                     SoVertexProperty *vertices = new SoVertexProperty;
00307                     int nVrtx = 0;
00308 
00309                     for (std::vector<SbVec3f>::const_iterator vit = (mit->second).begin (); vit != (mit->second).end (); ++vit) 
00310                     {   
00311                         vertices->vertex.set1Value (nVrtx++, (*vit));
00312                     }
00313                     
00314                     vertices->vertex.setNum (nVrtx);
00315                     SoPointSet *points = new SoPointSet;
00316                     points->vertexProperty.setValue (vertices);
00317                     points->numPoints.setValue (nVrtx);
00318                     trackSep->addChild (points);
00319 
00320                     if (m_connect.value ())
00321                     {                   
00322                         SoLineSet *track = new SoLineSet;
00323                         track->vertexProperty.setValue (vertices);
00324                         track->numVertices.setValue (nVrtx);
00325                         trackSep->addChild (track);
00326                     }
00327                     
00328                     sep->addChild (trackSep);               
00329                 }
00330             }
00331         }
00332         catch (cms::Exception& e)
00333         {
00334             e.append (" from VisPSimHitTwig::update(Ig3DRep*) ");
00335             e.append (this->name ());    
00336 
00337             if (this->m_onCmsException)
00338                 this->m_onCmsException (&e);
00339         }
00340         catch (lat::Error &e) 
00341         {
00342             if (this->m_onError)
00343                 this->m_onError (&e);
00344         }
00345         catch (std::exception &e) 
00346         {
00347             if (this->m_onException)
00348                 this->m_onException (&e);
00349         }
00350         catch (...) 
00351         {
00352             if (this->m_onUnhandledException)
00353                 this->m_onUnhandledException ();
00354         }
00355         rep->node ()->addChild (sep);
00356     }
00357 }
00358 
00359 void
00360 VisPSimHitTwig::update (IgLegoRep *rep)
00361 {
00362     // Get debugging dump.
00363     VisQueuedTwig::update (rep);
00364 }
00365 
00366 void
00367 VisPSimHitTwig::update (IgRPhiRep *rep)
00368 {
00369     // Get debugging dump.
00370     VisQueuedTwig::update (rep);
00371 
00372     if (! m_hits.empty ())
00373     {
00374         SoMaterial *mat = new SoMaterial;
00375         mat->diffuseColor.setValue (0.0, 0.0, 1.0);
00376         mat->emissiveColor.setValue(0.0, 0.0, 1.0);
00377 
00378         SoDrawStyle *drawStyle = new SoDrawStyle;
00379         drawStyle->pointSize = 6.0;
00380 
00381         SoSeparator *sep = new SoSeparator; // global
00382         sep->addChild (mat);
00383         sep->addChild (drawStyle);
00384 
00385         VisEventSetupService *esService = VisEventSetupService::get (state ());
00386         ASSERT (esService);
00387 
00388         try 
00389         {
00390             typedef std::map<int, std::vector <SbVec3f> > VisParticleMap;
00391             VisParticleMap pmap;
00392 
00393             for (std::vector<PSimHit>::const_iterator i = m_hits.begin (); i != m_hits.end (); ++i) 
00394             {
00395                 int ptype = (*i).trackId ();
00396                 
00397                 VisParticleMap::iterator mit = pmap.find (ptype);
00398                 
00399                 if (mit == pmap.end ())
00400                 {
00401                     std::vector<SbVec3f> vec;
00402                     bool done = pmap.insert (VisParticleMap::value_type (ptype, vec)).second;
00403                     if (done)
00404                     {
00405                         mit = pmap.find (ptype);
00406                     }
00407                 }
00408 
00409                 if (mit == pmap.end ())
00410                 {
00411                     throw std::runtime_error ("Cannot find particle type in the map!");
00412                 }               
00413                 DetId id = (*i).detUnitId ();
00414                 if (! id.null () && (id.det () == DetId::Tracker || id.det () == DetId::Muon))
00415                 {
00416                     GlobalPoint pos = esService->toGlobal ((*i).localPosition (), id);
00417                     (mit->second).push_back (SbVec3f (pos.x () / 100.0, pos.y () / 100.0, pos.z () / 100.0));
00418                 }
00419             }       
00420             if (! pmap.empty ())
00421             {
00422                 for (VisParticleMap::const_iterator mit = pmap.begin (); mit != pmap.end (); ++mit)
00423                 {
00424                     SoSeparator *trackSep = new SoSeparator; 
00425                     
00426                     QString label = QString ("ID = %1")
00427                                     .arg (mit->first);             
00428 
00429                     trackSep->setName (Ig3DBaseModel::encode (label.prepend ("Track with ")));
00430 
00431                     SoVertexProperty *vertices = new SoVertexProperty;
00432                     int nVrtx = 0;
00433 
00434                     for (std::vector<SbVec3f>::const_iterator vit = (mit->second).begin (); vit != (mit->second).end (); ++vit) 
00435                     {   
00436                         vertices->vertex.set1Value (nVrtx++, (*vit));
00437                     }
00438                     
00439                     vertices->vertex.setNum (nVrtx);
00440                     SoPointSet *points = new SoPointSet;
00441                     points->vertexProperty.setValue (vertices);
00442                     points->numPoints.setValue (nVrtx);
00443                     trackSep->addChild (points);
00444 
00445                     if (m_connect.value ()) 
00446                     {
00447                         SoLineSet *track = new SoLineSet;
00448                         track->vertexProperty.setValue (vertices);
00449                         track->numVertices.setValue (nVrtx);
00450                         trackSep->addChild (track);
00451                     }
00452                     
00453                     sep->addChild (trackSep);               
00454                 }
00455             }
00456         }
00457         catch (cms::Exception& e)
00458         {
00459             e.append (" from VisPSimHitTwig::update(IgRPhiRep*) ");
00460             e.append (this->name ());    
00461 
00462             if (this->m_onCmsException)
00463                 this->m_onCmsException (&e);
00464         }
00465         catch (lat::Error &e) 
00466         {
00467             if (this->m_onError)
00468                 this->m_onError (&e);
00469         }
00470         catch (std::exception &e) 
00471         {
00472             if (this->m_onException)
00473                 this->m_onException (&e);
00474         }
00475         catch (...) 
00476         {
00477             if (this->m_onUnhandledException)
00478                 this->m_onUnhandledException ();
00479         }
00480         rep->node ()->addChild (sep);
00481     }
00482 }
00483 
00484 void
00485 VisPSimHitTwig::update (IgRZRep *rep)
00486 {
00487     // Get debugging dump.
00488     VisQueuedTwig::update (rep);
00489 
00490     if (! m_hits.empty ())
00491     {
00492         SoMaterial *mat = new SoMaterial;
00493         mat->diffuseColor.setValue (0.0, 0.0, 1.0);
00494         mat->emissiveColor.setValue(0.0, 0.0, 1.0);
00495 
00496         SoDrawStyle *drawStyle = new SoDrawStyle;
00497         drawStyle->pointSize = 6.0;
00498 
00499         SoSeparator *sep = new SoSeparator; // global
00500         sep->addChild (mat);
00501         sep->addChild (drawStyle);
00502 
00503         VisEventSetupService *esService = VisEventSetupService::get (state ());
00504         ASSERT (esService);
00505 
00506         try 
00507         {
00508             typedef std::map<int, std::vector <SbVec3f> > VisParticleMap;
00509             VisParticleMap pmap;
00510 
00511             for (std::vector<PSimHit>::const_iterator i = m_hits.begin (); i != m_hits.end (); ++i) 
00512             {
00513                 int ptype = (*i).trackId ();
00514                 
00515                 VisParticleMap::iterator mit = pmap.find (ptype);
00516                 
00517                 if (mit == pmap.end ())
00518                 {
00519                     std::vector<SbVec3f> vec;
00520                     bool done = pmap.insert (VisParticleMap::value_type (ptype, vec)).second;
00521                     if (done)
00522                     {
00523                         mit = pmap.find (ptype);
00524                     }
00525                 }
00526 
00527                 if (mit == pmap.end ())
00528                 {
00529                     throw std::runtime_error ("Cannot find particle type in the map!");
00530                 }               
00531                 DetId id = (*i).detUnitId ();
00532                 if (! id.null () && (id.det () == DetId::Tracker || id.det () == DetId::Muon))
00533                 {
00534                     GlobalPoint pos = esService->toGlobal ((*i).localPosition (), id);
00535                     (mit->second).push_back (SbVec3f (pos.x () / 100.0, pos.y () / 100.0, pos.z () / 100.0));
00536                 }
00537             }       
00538             if (! pmap.empty ())
00539             {
00540                 for (VisParticleMap::const_iterator mit = pmap.begin (); mit != pmap.end (); ++mit)
00541                 {
00542                     SoSeparator *trackSep = new SoSeparator; 
00543                     
00544                     QString label = QString ("ID = %1")
00545                                     .arg (mit->first);             
00546 
00547                     trackSep->setName (Ig3DBaseModel::encode (label.prepend ("Track with ")));
00548 
00549                     SoVertexProperty *vertices = new SoVertexProperty;
00550                     int nVrtx = 0;
00551 
00552                     for (std::vector<SbVec3f>::const_iterator vit = (mit->second).begin (); vit != (mit->second).end (); ++vit) 
00553                     {   
00554                         vertices->vertex.set1Value (nVrtx++, (*vit));
00555                     }
00556                     
00557                     vertices->vertex.setNum (nVrtx);
00558                     SoPointSet *points = new SoPointSet;
00559                     points->vertexProperty.setValue (vertices);
00560                     points->numPoints.setValue (nVrtx);
00561                     trackSep->addChild (points);
00562 
00563                     if (m_connect.value ()) 
00564                     {
00565                         SoLineSet *track = new SoLineSet;
00566                         track->vertexProperty.setValue (vertices);
00567                         track->numVertices.setValue (nVrtx);
00568                         trackSep->addChild (track);
00569                     }
00570                     
00571                     sep->addChild (trackSep);               
00572                 }
00573             }
00574         }
00575         catch (cms::Exception& e)
00576         {
00577             e.append (" from VisPSimHitTwig::update(IgRZRep*) ");
00578             e.append (this->name ());    
00579 
00580             if (this->m_onCmsException)
00581                 this->m_onCmsException (&e);
00582         }
00583         catch (lat::Error &e) 
00584         {
00585             if (this->m_onError)
00586                 this->m_onError (&e);
00587         }
00588         catch (std::exception &e) 
00589         {
00590             if (this->m_onException)
00591                 this->m_onException (&e);
00592         }
00593         catch (...) 
00594         {
00595             if (this->m_onUnhandledException)
00596                 this->m_onUnhandledException ();
00597         }
00598         rep->node ()->addChild (sep);
00599     }
00600 }

Generated on Tue Jun 9 17:50:31 2009 for CMSSW by  doxygen 1.5.4