00001
00002 #include "VisReco/VisTracker/interface/VisTrajectorySeedTwig.h"
00003 #include "VisFramework/VisFrameworkBase/interface/VisTwigFactroyService.h"
00004 #include "VisFramework/VisFrameworkBase/interface/VisEventSelector.h"
00005 #include "VisFramework/VisFrameworkBase/interface/debug.h"
00006 #include "VisFramework/VisEventSetup/interface/VisEventSetupService.h"
00007
00008 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
00009 #include "FWCore/Framework/interface/Event.h"
00010 #include "FWCore/Framework/interface/EventSetup.h"
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00013
00014 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00015 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
00016 #include "DataFormats/GeometryVector/interface/LocalVector.h"
00017 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
00018 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00019 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00020 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00021
00022 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00023 #include "Geometry/Records/interface/GlobalTrackingGeometryRecord.h"
00024 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00025 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00026 #include "Iguana/Inventor/interface/IgSoArrow.h"
00027
00028 #include "Iguana/Models/interface/IgTextRep.h"
00029 #include "Iguana/GLModels/interface/Ig3DRep.h"
00030 #include "Iguana/GLModels/interface/IgRPhiRep.h"
00031 #include "Iguana/GLModels/interface/IgRZRep.h"
00032 #include "Iguana/GLModels/interface/IgLegoRep.h"
00033 #include "Iguana/Framework/interface/IgRepSet.h"
00034 #include "Iguana/Studio/interface/IgDocumentData.h"
00035 #include "Iguana/Studio/interface/IgQtAppStatusBarService.h"
00036 #include "Iguana/Studio/interface/IgQtLock.h"
00037
00038 #include <Inventor/nodes/SoMaterial.h>
00039 #include <Inventor/nodes/SoSeparator.h>
00040 #include <Inventor/nodes/SoDrawStyle.h>
00041 #include <qstring.h>
00042 #include <sstream>
00043 #include <iomanip>
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 namespace
00056 {
00057 VisQueuedTwig *
00058 createThisTwig (IgState *state, IgTwig *parent,
00059 const std::string &name,
00060 const std::string &friendlyName,
00061 const std::string &modLabel,
00062 const std::string &instanceName,
00063 const std::string &processName)
00064 {
00065 IgTwig *rootTwig = IgDocumentData::get (state)->root ();
00066 IgTwig *eventTwig = 0;
00067 eventTwig = rootTwig->lookup ("/Objects/CMS Event and Detector/Tracker Event/Trajectory Seeds");
00068
00069 if (! eventTwig)
00070 eventTwig = parent;
00071
00072 return new VisTrajectorySeedTwig (state, eventTwig, "[N/A] Seed (" + name + ")",
00073 friendlyName, modLabel, instanceName, processName);
00074 }
00075 }
00076
00077
00078
00079
00080 VisTrajectorySeedTwig::VisTrajectorySeedTwig (IgState *state, IgTwig *parent,
00081 const std::string &name ,
00082 const std::string &friendlyName ,
00083 const std::string &moduleLabel ,
00084 const std::string &instanceName ,
00085 const std::string &processName )
00086 : VisQueuedTwig (state, parent, name),
00087 m_text ("no info"),
00088 m_friendlyName (friendlyName),
00089 m_moduleLabel (moduleLabel),
00090 m_instanceName (instanceName),
00091 m_processName (processName),
00092 m_seedRescaleFactor (state, lat::CreateCallback (this, &VisTrajectorySeedTwig::twigChanged))
00093 {
00094 VisTwigFactroyService *tfService = VisTwigFactroyService::get (state);
00095 if (! tfService)
00096 {
00097 tfService = new VisTwigFactroyService (state);
00098 }
00099 edm::TypeID seedID (typeid (TrajectorySeedCollection));
00100 tfService->registerTwig (seedID.friendlyClassName (), &createThisTwig);
00101 }
00102
00103 void
00104 VisTrajectorySeedTwig::twigChanged (void)
00105 { IgRepSet::invalidate (this, SELF_MASK); }
00106
00107 void
00108 VisTrajectorySeedTwig::onNewEvent (const edm::Event &event,
00109 const edm::EventSetup &eventSetup)
00110 {
00111
00112 VisQueuedTwig::onNewEvent (event, eventSetup);
00113
00114 m_hitPoints.clear ();
00115 m_seedPositions.clear ();
00116 m_seedDirections.clear ();
00117
00118 m_text = (QString ("Run %1, Event %2, LS %3, Orbit %4, BX %5")
00119 .arg (event.id ().run ())
00120 .arg (event.id ().event ())
00121 .arg (event.luminosityBlock ())
00122 .arg (event.orbitNumber ())
00123 .arg (event.bunchCrossing ())
00124 .latin1 ());
00125
00126 std::vector<edm::Handle<TrajectorySeedCollection> > seedCollections;
00127
00128 try
00129 {
00130 if ((! m_friendlyName.empty ()) || (! m_moduleLabel.empty ()) || (! m_instanceName.empty ()) || (! m_processName.empty ()))
00131 {
00132 VisEventSelector visSel (m_friendlyName, m_moduleLabel, m_instanceName, m_processName);
00133 event.getMany (visSel, seedCollections);
00134 }
00135 }
00136 catch (cms::Exception& e)
00137 {
00138 e.append (" from VisTrajectorySeedTwig: ");
00139 e.append (this->name ());
00140
00141 if (this->m_onCmsException)
00142 this->m_onCmsException (&e);
00143 }
00144 catch (lat::Error &e)
00145 {
00146 if (this->m_onError)
00147 this->m_onError (&e);
00148 }
00149 catch (std::exception &e)
00150 {
00151 if (this->m_onException)
00152 this->m_onException (&e);
00153 }
00154 catch (...)
00155 {
00156 if (this->m_onUnhandledException)
00157 this->m_onUnhandledException ();
00158 }
00159
00160 try
00161 {
00162 eventSetup.get<GlobalTrackingGeometryRecord> ().get (m_trackerGeometry);
00163 }
00164 catch (cms::Exception& e)
00165 {
00166 e.append (" from VisTrajectorySeedTwig: ");
00167 e.append (this->name ());
00168
00169 if (this->m_onCmsException)
00170 this->m_onCmsException (&e);
00171 }
00172 catch (lat::Error &e)
00173 {
00174 if (this->m_onError)
00175 this->m_onError (&e);
00176 }
00177 catch (std::exception &e)
00178 {
00179 if (this->m_onException)
00180 this->m_onException (&e);
00181 }
00182 catch (...)
00183 {
00184 if (this->m_onUnhandledException)
00185 this->m_onUnhandledException ();
00186 }
00187
00188
00189 try
00190 {
00191 eventSetup.get<IdealMagneticFieldRecord> ().get (m_mgField);
00192 }
00193 catch (cms::Exception& e)
00194 {
00195 e.append (" from VisTrajectorySeedTwig: ");
00196 e.append (this->name ());
00197
00198 if (this->m_onCmsException)
00199 this->m_onCmsException (&e);
00200 }
00201 catch (lat::Error &e)
00202 {
00203 if (this->m_onError)
00204 this->m_onError (&e);
00205 }
00206 catch (std::exception &e)
00207 {
00208 if (this->m_onException)
00209 this->m_onException (&e);
00210 }
00211 catch (...)
00212 {
00213 if (this->m_onUnhandledException)
00214 this->m_onUnhandledException ();
00215 }
00216
00217 if (! seedCollections.empty ())
00218 {
00219 VisEventSetupService *esService = VisEventSetupService::get (state ());
00220 ASSERT (esService);
00221
00222 if (m_trackerGeometry.isValid ())
00223 {
00224 if (m_mgField.isValid ())
00225 {
00226 LOG (2, trace, LFfwvis, "OK: seedCollections is not empty and GlobalTrackingGeometry is valid===>>>\n");
00227
00228 std::vector<edm::Handle<TrajectorySeedCollection> >::iterator i;
00229 std::vector<edm::Handle<TrajectorySeedCollection> >::iterator end;
00230
00231 LOG (2, trace, LFfwvis, "There are " << seedCollections.size () << " seed collections.\n");
00232
00233 int tc = 1;
00234 for (i = seedCollections.begin (), end = seedCollections.end (); i != end; ++i)
00235 {
00236 try
00237 {
00238 const TrajectorySeedCollection c = *(*i).product ();
00239 QString sizeStr = (QString ("%1").arg (c.size ()));
00240 QString nameStr = QString (this->name ());
00241 int ib = nameStr.find ("[");
00242 int ie = nameStr.find ("]");
00243 nameStr.replace (ib + 1, ie - 1, sizeStr);
00244 this->name (nameStr);
00245
00246 LOG (2, trace, LFfwvis, ": seed collection has " << c.size () << " seeds\n");
00247
00248 for (TrajectorySeedCollection::const_iterator seed = c.begin (), seedEnd = c.end (); seed != seedEnd; ++seed)
00249 {
00250 LOG (2, trace, LFfwvis, "\nSeed with rechitsize = " << (*seed).nHits () << "\n");
00251 std::vector<SbVec3f> mySeed;
00252
00253 int nRecHit = 1;
00254 for (edm::OwnVector<TrackingRecHit>::const_iterator it = seed->recHits ().first, itEnd = seed->recHits ().second; it != itEnd; ++it)
00255 {
00256 if ((*it).isValid () && !(*it).geographicalId ().null ())
00257 {
00258 GlobalPoint gp = esService->globalPosition (&(*it));
00259 float x = gp.x ();
00260 float y = gp.y ();
00261 float z = gp.z ();
00262
00263 LOG (2, trace, LFfwvis, "#" << nRecHit++ << ": " << "(" << x << ", " << y << ", " << z << ")"
00264 << " on module =" << (it)->geographicalId ().rawId () << "\n");
00265 mySeed.push_back (SbVec3f (x, y, z));
00266 }
00267 }
00268 m_hitPoints.push_back (mySeed);
00269
00270 LOG (2, trace, LFfwvis, "\nConvert TrajectoryState \n");
00271
00272 TrajectoryStateTransform tsTransform;
00273
00274 PTrajectoryStateOnDet pTSOD = seed->startingState ();
00275 DetId seedDetId (pTSOD.detId ());
00276
00277 const GeomDet* gdet = esService->idToDet (seedDetId);
00278
00279 TrajectoryStateOnSurface tsos = tsTransform.transientState (pTSOD, &(gdet->surface ()),
00280 &*m_mgField);
00281
00282 if (tsos.isValid ())
00283 {
00284 float x = tsos.globalPosition ().x () / 100.0;
00285 float y = tsos.globalPosition ().y () / 100.0;
00286 float z = tsos.globalPosition ().z () / 100.0;
00287
00288 float dirx = tsos.globalDirection ().x () / 100.0;
00289 float diry = tsos.globalDirection ().y () / 100.0;
00290 float dirz = tsos.globalDirection ().z () / 100.0;
00291
00292 LOG (2, trace, LFfwvis, "seed"<< "Pos: (" << x << ", " << y << ", " << z
00293 << "),\n Dir: (" << dirx << ", " << diry << ", " << dirz << ") ");
00294
00295 m_seedPositions.push_back (SbVec3f (x, y, z));
00296 m_seedDirections.push_back (SbVec3f (dirx, diry, dirz));
00297 }
00298 }
00299 }
00300 catch (cms::Exception& e)
00301 {
00302 e.append (" from VisTrajectorySeedTwig: ");
00303 e.append (this->name ());
00304
00305 if (this->m_onCmsException)
00306 this->m_onCmsException (&e);
00307 }
00308 catch (lat::Error &e)
00309 {
00310 if (this->m_onError)
00311 this->m_onError (&e);
00312 }
00313 catch (std::exception &e)
00314 {
00315 if (this->m_onException)
00316 this->m_onException (&e);
00317 }
00318 catch (...)
00319 {
00320 if (this->m_onUnhandledException)
00321 this->m_onUnhandledException ();
00322 }
00323 }
00324 }
00325 }
00326 }
00327
00328 VisQueuedTwig::onBaseInvalidate ();
00329
00330 if (IgQtAppStatusBarService *sbar = IgQtAppStatusBarService::get (state ()))
00331 sbar->setMessage (QString (m_text));
00332 }
00333
00334 void
00335 VisTrajectorySeedTwig::update (IgTextRep *rep)
00336 {
00337
00338 VisQueuedTwig::update (rep);
00339
00340
00341 std::ostringstream text;
00342
00343 text << setiosflags (std::ios::showpoint | std::ios::fixed);
00344 text.setf (std::ios::right, std::ios::adjustfield);
00345
00346 text << "Trajectory Seed = ";
00347
00348 try
00349 {
00350 unsigned int seed_size = m_seedPositions.size ();
00351 text << std::setw (3) << seed_size;
00352
00353 for (unsigned int nt = 0; nt < seed_size; ++nt)
00354 {
00355 double seed_pt = sqrt (m_seedDirections [nt][0] * m_seedDirections [nt][0] + m_seedDirections [nt][1] * m_seedDirections [nt][1]);
00356
00357 std::ostringstream out_pt;
00358 out_pt << seed_pt;
00359 text << "<table width='100%' border=1>"
00360 << "<TR align = center>"
00361 << "<TD COLSPAN =4>Seed number = " << nt
00362 << " track pt = " << out_pt.str ()
00363 << "</TD></TR><TR align = center>"
00364 << "<TH>NumberRecHit</TH>"
00365 << "<TH>GlobalPos</TH>"
00366 << "</TR>";
00367
00368 int n_rechit = 0;
00369 if (nt < m_hitPoints.size ())
00370 for (std::vector<SbVec3f>::const_iterator ir = m_hitPoints[nt].begin (); ir != m_hitPoints[nt].end (); ++ir)
00371 {
00372 SbVec3f xyzPoint = (*ir);
00373 float x = xyzPoint [0];
00374 float y = xyzPoint [1];
00375 float z = xyzPoint [2];
00376
00377 text << "<TR align = right>"
00378 << "<TD>" << std::setw (3) << n_rechit++<< "</TD>"
00379 << "<TD>" << std::setw (6) << std::setprecision (3) << x << "," << y << "," << z << "</TD></TR>";
00380 }
00381 text << "</table>";
00382 }
00383 }
00384 catch (cms::Exception& e)
00385 {
00386 e.append (" from VisTrajectorySeedTwig: ");
00387 e.append (this->name ());
00388
00389 if (this->m_onCmsException)
00390 this->m_onCmsException (&e);
00391 }
00392 catch (lat::Error &e)
00393 {
00394 if (this->m_onError)
00395 this->m_onError (&e);
00396 }
00397 catch (std::exception &e)
00398 {
00399 if (this->m_onException)
00400 this->m_onException (&e);
00401 }
00402 catch (...)
00403 {
00404 if (this->m_onUnhandledException)
00405 this->m_onUnhandledException ();
00406 }
00407
00408 IgQtLock ();
00409 rep->setText (text.str ());
00410 }
00411
00412 void
00413 VisTrajectorySeedTwig::update (Ig3DRep *rep)
00414 {
00415
00416 VisQueuedTwig::update (rep);
00417
00418 IgQtLock ();
00419 rep->clear ();
00420
00421 if (! m_seedPositions.empty ())
00422 {
00423 SoSeparator *sep = new SoSeparator;
00424
00425 SoMaterial *recomat = new SoMaterial;
00426 recomat->diffuseColor.setValue (0.6, 1.0, 0.0);
00427 sep->addChild (recomat);
00428
00429 std::vector<SbVec3f>::const_iterator idir = m_seedDirections.begin ();
00430
00431 for (std::vector<SbVec3f>::const_iterator i = m_seedPositions.begin (); i != m_seedPositions.end (); ++i)
00432 {
00433 IgSoArrow* seedArrow = new IgSoArrow;
00434 seedArrow->tail.setValue (*i);
00435
00436 float x1 ((*i)[0] + m_seedRescaleFactor.value () *(*idir)[0]);
00437 float y1 ((*i)[1] + m_seedRescaleFactor.value () *(*idir)[1]);
00438 float z1 ((*i)[2] + m_seedRescaleFactor.value () *(*idir)[2]);
00439
00440 SbVec3f tipPos( SbVec3f(x1, y1, z1) );
00441 seedArrow->tip.setValue (tipPos);
00442 sep->addChild (seedArrow);
00443 idir++;
00444 }
00445 rep->node ()->addChild (sep);
00446 }
00447 }
00448
00449 void
00450 VisTrajectorySeedTwig::update (IgRPhiRep *rep)
00451 {
00452
00453 VisQueuedTwig::update (rep);
00454
00455 IgQtLock ();
00456 rep->clear ();
00457
00458 if (! m_seedPositions.empty ())
00459 {
00460 SoSeparator *sep = new SoSeparator;
00461
00462 SoMaterial *recomat = new SoMaterial;
00463 recomat->diffuseColor.setValue (0.6, 1.0, 0.0);
00464 sep->addChild (recomat);
00465
00466 std::vector<SbVec3f>::const_iterator idir = m_seedDirections.begin ();
00467
00468 for (std::vector<SbVec3f>::const_iterator i = m_seedPositions.begin (); i != m_seedPositions.end (); ++i)
00469 {
00470 IgSoArrow* seedArrow = new IgSoArrow;
00471 seedArrow->tail.setValue (*i);
00472
00473 float x1 ((*i)[0] + m_seedRescaleFactor.value () *(*idir)[0]);
00474 float y1 ((*i)[1] + m_seedRescaleFactor.value () *(*idir)[1]);
00475 float z1 ((*i)[2] + m_seedRescaleFactor.value () *(*idir)[2]);
00476
00477 SbVec3f tipPos (SbVec3f (x1, y1, z1));
00478 seedArrow->tip.setValue (tipPos);
00479 sep->addChild (seedArrow);
00480 idir++;
00481 }
00482 rep->node ()->addChild (sep);
00483 }
00484 }
00485
00486 void
00487 VisTrajectorySeedTwig::update (IgRZRep *rep)
00488 {
00489
00490 VisQueuedTwig::update (rep);
00491
00492 IgQtLock ();
00493 rep->clear ();
00494
00495 if (! m_seedPositions.empty ())
00496 {
00497 SoSeparator *sep = new SoSeparator;
00498
00499 SoMaterial *recomat = new SoMaterial;
00500 recomat->diffuseColor.setValue (0.6, 1.0, 0.0);
00501 sep->addChild (recomat);
00502
00503 std::vector<SbVec3f>::const_iterator idir = m_seedDirections.begin ();
00504
00505 for (std::vector<SbVec3f>::const_iterator i = m_seedPositions.begin (); i != m_seedPositions.end (); ++i)
00506 {
00507 IgSoArrow* seedArrow = new IgSoArrow;
00508 float iyy = sqrt ((*i)[0]*(*i)[0] + (*i)[1]*(*i)[1]);
00509 if ((*i)[1] < 0) iyy = -iyy;
00510
00511 SbVec3f rzpos(0, iyy, (*i)[2]);
00512
00513 seedArrow->tail.setValue (rzpos);
00514
00515 float x1 ((*i)[0] + m_seedRescaleFactor.value () *(*idir)[0] );
00516 float y1 ((*i)[1] + m_seedRescaleFactor.value () *(*idir)[1] );
00517 float z1 ((*i)[2] + m_seedRescaleFactor.value () *(*idir)[2] );
00518
00519 float irr = sqrt (x1*x1 + y1*y1);
00520 if (y1 < 0) irr = -irr;
00521 SbVec3f tipPos (SbVec3f (0, irr, z1));
00522 seedArrow->tip.setValue (tipPos);
00523 sep->addChild (seedArrow);
00524 idir++;
00525 }
00526 rep->node ()->addChild (sep);
00527 }
00528 }
00529
00530 void
00531 VisTrajectorySeedTwig::update (IgLegoRep *rep)
00532 {
00533
00534
00535 VisQueuedTwig::update (rep);
00536
00537 IgQtLock ();
00538 rep->clear ();
00539
00540
00541 }
00542