ATLAS Offline Software
Loading...
Searching...
No Matches
VP1VertexCollection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7// //
8// Implementation of class VP1VertexCollection //
9// //
10// Author: Andreas.Wildauer@cern.ch //
11// Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
12// Ben Zastovnik //
13// Initial version: July 2008 //
14// //
16
25#include "VP1Base/VP1Msg.h"
26
27#include <QStringList>
28#include <QComboBox>
29
30#include <Inventor/nodes/SoTranslation.h>
31#include <Inventor/nodes/SoSeparator.h>
32#include <Inventor/nodes/SoSphere.h>
33#include <Inventor/nodes/SoPointSet.h>
34#include <Inventor/nodes/SoVertexProperty.h>
35#include <Inventor/nodes/SoMatrixTransform.h>
36#include <Inventor/nodes/SoMaterial.h>
37#include <Inventor/SbMatrix.h>
38#include <Inventor/SoPath.h>
39
46#include "GaudiKernel/SystemOfUnits.h"
47
48#include <iostream>
49
50
51//____________________________________________________________________
53{
54 QList<VP1StdCollection*> l;
55 for (const QString& key : VP1SGContentsHelper(controller->systemBase()).getKeys<VxContainer>()) {
56 VP1VertexCollection * col = new VP1VertexCollection(controller,key);
57 col->init();
58 l << col;
59 }
60 return l;
61}
62
63//____________________________________________________________________
65public:
68 QString key;
69
70 // std::map <SoNode *, const Trk::VxCandidate *> nodeToVertexMap;//fixme: obsolete
72 QList<const Trk::Track*>& tracks,
73 QList<const Trk::TrackParticleBase*>& trackparticles);//fixme: use
74
76 public:
78 ~VertexHandle() { if (m_sep) m_sep->unref(); }
79
81
83 if (!m_attached) {
84 if (m_sep)
85 m_sep->removeAllChildren();
86 return;
87 }
88 if (!m_sep) {
89 m_sep = new SoSeparator;
90 m_sep->ref();
91 }
92 if (m_sep->getNumChildren()>0)
93 m_sep->removeAllChildren();
94 SoMaterial* mat = determineMaterial();
95 // std::cout<<"This: "<<this<<" Got material at:"<<mat<<", m_randommaterial="<<m_randommaterial<<std::endl;
96 m_sep->addChild ( mat );
98 }
99
100 SoMaterial * determineMaterial() {
101 switch(collHandle()->colourBy()) {
103 {
104 if (!m_randommaterial) {
105 m_randommaterial = new SoMaterial;
106 m_randommaterial->ref();
108 }
109 return m_randommaterial;
110 }
112 default:
113 return collHandle()->material();
114 }
115 }
116
118 if (m_attached)
119 return;
120 m_attached = true;
122 collSep->addChild(m_sep);
123 }
124 void ensureDetached(SoSeparator * collSep) {
125 if (!m_attached)
126 return;
127 m_attached = false;
128 if (m_sep)
129 collSep->removeChild(m_sep);
130 }
131
132 const Trk::VxCandidate * vertex() const { return m_vertex; }
133 SoSeparator * sep() const { return m_sep; }
134 bool attached() const { return m_attached; }
151 // std::cout<<"rerandomiseRandomMaterial"<<std::endl;
152 if ( !m_randommaterial )//We will anyway rerandomize it when we need it
153 return;
154
155 double r2 = 0.3*0.3;
156 unsigned i(0);
157 double r,g,b;
158 bool ok;
159 while (true) {
160 r = (rand() / static_cast<double>(RAND_MAX));
161 g = (rand() / static_cast<double>(RAND_MAX));
162 b = (rand() / static_cast<double>(RAND_MAX));
163 ok = true;
164 //For now we make sure that we avoid black and red. This should be updated from bgd and highlight col automatically! (fixme).
165 // -> and we should probably also make sure that tracks close in (eta,phi) are well separated in colour-space.
166 if ( (r-1.0)*(r-1.0)+g*g+b*b < r2*0.5 )//avoid red (distance)
167 ok = false;
168 else if ( r*r/(r*r+g*g+b*b) > 0.8 )//avoid red (angle)
169 ok = false;
170 else if ( r*r+g*g+b*b < r2*2.0 )//avoid black
171 ok = false;
172 if (ok)
173 break;
174 ++i;
175 if (i>50 ) {
176 r2 *= 0.99;//To avoid problem in case we add too many forbidden spheres.
177 if (i>1000) {
178 //Just a safety
179 collHandle()->systemBase()->message("TrackHandleBase::rerandomiseRandomMaterial Warning: Random colour could"
180 " not be selected such as to satisfy all separation criteria");
181 break;
182 }
183 }
184 }
185 // std::cout<<"rerandomiseRandomMaterial - setting to"<<r<<","<<g<<","<<b<<std::endl;
186
188 0.15, /*brightness*/
189 0.0 /*transparency*/);
190 }
191
192 private:
196 SoSeparator* m_sep;
197 SoMaterial* m_randommaterial;
199 Amg::Vector3D pos = m_vertex->recVertex().position();
200 double r = controller->reconVertexRepresentation();
201 if (r==0) {
202 SoPointSet * points = new SoPointSet;
203 SoVertexProperty * vertices = new SoVertexProperty;
204 vertices->vertex.set1Value(0,pos.x(), pos.y(), pos.z());
205 points->numPoints=1;
206 points->vertexProperty.setValue(vertices);
207 m_sep->addChild(points);
208 return;
209 }
210
211 SoTranslation * translation = new SoTranslation;
212 translation->translation.setValue ( pos.x(), pos.y(), pos.z() );
213 m_sep->addChild ( translation );
214
215 if (r>0) {
216 //Transform sphere into an ellipsoid using the error matrix:
217// const Trk::CovarianceMatrix& errMat = m_vertex->recVertex().errorPosition().covariance();
218 const AmgSymMatrix(3)& errMat = m_vertex->recVertex().covariancePosition(); // see https://svnweb.cern.ch/trac/atlasoff/browser/Tracking/TrkEvent/VxVertex/trunk/VxVertex/RecVertex.h
219
220 //FIXME: std::min hack for now!!
221 double a(errMat(0,0)), b(errMat(0,1)), c(errMat(0,2)),
222 d(errMat(1,1)), e(errMat(1,2)),
223 f(errMat(2,2));
224 double det = a*(d*f-e*e) + 2*b*c*e - d*c*c-f*b*b;
225 if (det>0) {
226 double sixthrootofdet = exp(log(det)/6.0);
227 double invdet = 1.0/sixthrootofdet;
228 a *= invdet;
229 b *= invdet;
230 c *= invdet;
231 d *= invdet;
232 e *= invdet;
233 f *= invdet;
234 SbMatrix sbMat(a,b,c,0,
235 b,d,e,0,
236 c,e,f,0,
237 0,0,0,1);
238 SoMatrixTransform * matTrans = new SoMatrixTransform();
239 matTrans->matrix.setValue(sbMat);
240 m_sep->addChild (matTrans);
241 } else {
242 //fixme: warn
243 }
244 }
245
246 SoSphere * sphere = new SoSphere;
247 sphere->radius = fabs(r);
248 m_sep->addChild ( sphere );
249 }
250 };
252 return vh->type() & controller->reconCutAllowedTypes();//nb: cache allowed types val?
253 }
254
256 if (cut(vh,controller))
257 vh->ensureAttached(theclass->collSep(),controller);
258 else
259 vh->ensureDetached(theclass->collSep());
260 }
261
262 QList<VertexHandle*> vertices;
263 //Extra widgets:
264 QComboBox * comboBox_colourby = nullptr;
265 static QString comboBoxEntry_ColourByCollection() { return "Uniform"; }
266 static QString comboBoxEntry_ColourByRandom() { return "Random"; }
267
268 SoMaterial * m_randommaterial = nullptr;
269};
270
271
272//____________________________________________________________________
274 : VP1StdCollection(controller->systemBase(),"VP1VertexCollection_"+key), m_d(new Imp),
276{
277 m_d->theclass = this;
278 m_d->controller = controller;
279 m_d->key = key;
280 m_d->comboBox_colourby = new QComboBox;
281 connect(controller,SIGNAL(reconVertexRepresentationChanged(const double&)),this,SLOT(updateAllShapes()));
282 connect(controller,SIGNAL(reconCutAllowedTypesChanged(VertexCommonFlags::ReconVertexTypeFlags)),this,SLOT(recheckAllCuts()));
283 connect(this,SIGNAL(tracksFromVertexChanged(QList<std::pair<const SoMaterial*,QList<const Trk::Track*> > >&)),
284 dynamic_cast<VP1VertexSystem*>(controller->systemBase()),SLOT(updateVertexToTracks(QList<std::pair<const SoMaterial*,QList<const Trk::Track*> > >&))); // FIXME - only for data!
285
286}
287
288//____________________________________________________________________
290{
291 for (Imp::VertexHandle* vh : m_d->vertices)
292 delete vh;
293
294 // delete m_d->comboBox_colourby; DONE by baseclass
295 delete m_d;
296}
297
299{
300 VP1StdCollection::init();//this call is required
301
302 // setupSettingsFromController(common()->controller());
303
304 //Setup colour by:
305 m_d->comboBox_colourby->setToolTip("Determine how vertices from this collection are coloured.");
306 QStringList l;
309 m_d->comboBox_colourby->addItems(l);
310 m_d->comboBox_colourby->setCurrentIndex(0);//corresponds to per collection.
311 connect(m_d->comboBox_colourby,SIGNAL(currentIndexChanged(int)),
312 this,SLOT(colourByComboBoxItemChanged()));
313
314 connect(this,SIGNAL(visibilityChanged(bool)),this,SLOT(collVisibilityChanged(bool)));
316
317 connect(m_d->controller,SIGNAL(rerandomise()),this,SLOT(rerandomiseRandomVertexColours()));
318
319}
320
322{
323 messageVerbose("setColourBy called");
324 if (m_colourby==cb)
325 return;
326 messageVerbose("setColourBy ==> Changed");
327 m_colourby=cb;
328
329 //Update gui combobox:
330 QString targetText;
331 switch(cb) {
332 case COLOUR_RANDOM:
334 break;
335 default:
338 break;
339 }
340 if (targetText!=m_d->comboBox_colourby->currentText()) {
341 int i = m_d->comboBox_colourby->findText(targetText);
342 if (i>=0&&i<m_d->comboBox_colourby->count()) {
343 bool save = m_d->comboBox_colourby->blockSignals(true);
344 m_d->comboBox_colourby->setCurrentIndex(i);
345 m_d->comboBox_colourby->blockSignals(save);
346 } else {
347 message("ERROR: Problems finding correct text in combo box");
348 }
349 }
350
351 // //Actual material updates (could do this more efficiently by just updating material, but shouldn't matter for vertices)
353
354 // Now work out what colours to send to the track system
355 QList<const Trk::Track*> tracks;
356 QList<const Trk::TrackParticleBase*> trackparticles;
357 QList< std::pair<const SoMaterial*, QList< const Trk::Track*> > > colAndTracks;
358 for (Imp::VertexHandle* vh : m_d->vertices){
359 m_d->findAssociatedTracks(vh->vertex(),tracks,trackparticles);
360 const SoMaterial* mat = vh->attached()?vh->determineMaterial():0; // store either the material, or 0 for hidden vertices
361 colAndTracks.append(std::pair<const SoMaterial*, QList< const Trk::Track*> >(mat, tracks));
362 }
363 emit tracksFromVertexChanged(colAndTracks);
364
365}
366
367//____________________________________________________________________
369{
370 return QList<QWidget*>() << m_d->comboBox_colourby;
371
372}
373
374//____________________________________________________________________
376{
377 messageVerbose("Collection detail level combo box changed index");
378 if (m_d->comboBox_colourby->currentText()==Imp::comboBoxEntry_ColourByRandom())
380 else
382}
383
384//____________________________________________________________________
386{
387 return m_d->key;
388}
389
390//____________________________________________________________________
392 QList<const Trk::Track*>& tracks,
393 QList<const Trk::TrackParticleBase*>& trackparticles)
394{
395 tracks.clear();
396 trackparticles.clear();
397 if (!vtx)
398 return;
399
400 const std::vector<Trk::VxTrackAtVertex*> * tmpVxTAVtx = vtx->vxTrackAtVertex();
401 for ( std::vector<Trk::VxTrackAtVertex*>::const_iterator itr = tmpVxTAVtx->begin(); itr != tmpVxTAVtx->end(); ++itr) {
402 Trk::ITrackLink * link = *itr ? (*itr)->trackOrParticleLink() : 0;
403 if (!link) continue;
404
405 //Attempt to get Trk::TrackParticleBase:
406
407 Trk::LinkToTrackParticleBase* linkToTrackParticle = dynamic_cast<Trk::LinkToTrackParticleBase*>(link);
408 const Trk::TrackParticleBase * tp((linkToTrackParticle && linkToTrackParticle->isValid())?linkToTrackParticle->cachedElement():0);
409
410 //Attempt to get Trk::Track - directly or from the track particle:
411 const Trk::Track* trk(0);
412 if (tp) {
413 trk = tp->originalTrack();
414 } else {
415 Trk::LinkToTrack* linkToTrack = dynamic_cast<Trk::LinkToTrack*>(link);
416 if (linkToTrack && linkToTrack->isValid())
417 trk = linkToTrack->cachedElement();
418 }
419
420 //Store:
421 if (tp)
422 trackparticles << tp;
423 if (trk)
424 tracks << trk;
425 }
426
427}
428
429//____________________________________________________________________
431{
432 VP1MaterialButton::setMaterialParameters( m, 0.66667/*red*/, 1.0/*green*/, 0.0/*blue*/, 0.15 /*brightness*/ );
433}
434
435//____________________________________________________________________
437{
439 QList<const Trk::Track*> tracks;
440 QList<const Trk::TrackParticleBase*> trackparticles;
441 QList< std::pair<const SoMaterial*, QList< const Trk::Track*> > > colAndTracks;
442 for (Imp::VertexHandle* vh : m_d->vertices){
443 m_d->recheckCut(vh);
444 m_d->findAssociatedTracks(vh->vertex(),tracks,trackparticles);
445 const SoMaterial* mat = vh->attached()?vh->determineMaterial():0; // store either the material, or 0 for hidden vertices
446 colAndTracks.append(std::pair<const SoMaterial*, QList< const Trk::Track*> >(mat, tracks));
447 }
448 emit tracksFromVertexChanged(colAndTracks);
450}
451
452//____________________________________________________________________
454{
455 messageVerbose("VP1VertexCollection::updateAllShapes()");
456
458 for (Imp::VertexHandle* vh : m_d->vertices)
459 vh->updateShapes(m_d->controller);
461}
462
463//____________________________________________________________________
465{
466 const VxContainer* vxContainer;
467 if (!VP1SGAccessHelper(systemBase()).retrieve(vxContainer, m_d->key))
468 return false;
469
470 int i(0);
471 for ( VxContainer::const_iterator vtxItr=vxContainer->begin(); vtxItr != vxContainer->end() ; ++vtxItr ) {
472 const Trk::VxCandidate* theVxCandidate = *vtxItr;
473 if (!theVxCandidate)
474 continue;
475 m_d->vertices << new Imp::VertexHandle(theVxCandidate, this);
476 if (!(i++%20))
478 }
479
481 //TODO: Option in controller for colouring first vertex in the
482 //collection (the first is the identified primary vertex), with a
483 //distinct colour.
484
485 return true;
486}
487
488//____________________________________________________________________
489QStringList VP1VertexCollection::infoOnClicked(SoPath* pickedPath)
490{
491 static_cast<IVP13DSystem*>(systemBase())->deselectAll();
492
493 //Get Trk::VxCandidate pointer associated with the clicked node:
494 while (pickedPath->getLength()>3&&pickedPath->getNodeFromTail(0)->getTypeId()!=SoSeparator::getClassTypeId())
495 pickedPath->pop();
496 if (pickedPath->getNodeFromTail(0)->getTypeId()!=SoSeparator::getClassTypeId())
497 return QStringList() << "ERROR: Could not get vertex information for picked Node (1)";
498 SoSeparator * pickedSep = static_cast<SoSeparator *>(pickedPath->getNodeFromTail(0));
499
500 Imp::VertexHandle*vertexHandle(0);
501 for (Imp::VertexHandle*vh : m_d->vertices) {
502 if (vh->sep()==pickedSep) {
503 vertexHandle = vh;
504 break;
505 }
506 }
507 if (!vertexHandle)
508 return QStringList() << "ERROR: Could not get vertex information for picked Node (1)";
509 const Trk::VxCandidate * vtx = vertexHandle->vertex();
510
511 QStringList l;
512 if (m_d->controller->printInfoOnClick()) {
513
514 //Make output:
515 l <<"Reconstructed vertex from collection "+text()+":" ;
516 l << "--Position [CLHEP::mm]: ("+str(vtx->recVertex().position().x()/Gaudi::Units::mm)+", "+
517 str(vtx->recVertex().position().y()/Gaudi::Units::mm)+", "+str(vtx->recVertex().position().z()/Gaudi::Units::mm)+")";
518
519 //Track associations. (in the future we will use them to tell the
520 //track system to e.g. colour tracks by vertex - for now just print the number of those):
521 QList<const Trk::Track*> tracks;
522 QList<const Trk::TrackParticleBase*> trackparticles;
523 m_d->findAssociatedTracks(vtx,tracks,trackparticles);
524 l << "--#Tracks used: "+str(vtx->vxTrackAtVertex()->size());
525 if (m_d->controller->printVerboseInfoOnClick()) {
526 l << "--#Trk::Track's: "+str(tracks.count());
527 l << "--#Trk::TrackParticleBase's: "+str(trackparticles.count());
528 l <<"======== Dump ========";
529 std::ostringstream s;
530 vtx->dump(s);
531 l << QString(s.str().c_str()).split('\n');
532 l <<"======================";
533 }
534 }
535
536 if (m_d->controller->zoomOnClick()) {
537 std::set<SoCamera*> cameras = static_cast<IVP13DSystem*>(systemBase())->getCameraList();
538 std::set<SoCamera*>::iterator it,itE = cameras.end();
539 for (it=cameras.begin();it!=itE;++it)
540 VP1CameraHelper::animatedZoomToPath(*it,collSep(),pickedPath,2.0,1.0);
541 }
542
543 return l;
544}
545
546//____________________________________________________________________
548{
549 if (VP1Msg::verbose())
550 messageVerbose("VP1VertexCollection::collVisibilityChanged => "+str(vis));
551 if (vis) {
553 } else {
554 QList< std::pair<const SoMaterial*, QList< const Trk::Track*> > > colAndTracks;
555 emit tracksFromVertexChanged(colAndTracks);
556 }
557}
558
559//____________________________________________________________________
561{
562 if (!isLoaded())
563 return;
564 messageVerbose("rerandomiseRandomVertexColours start");
566 for (Imp::VertexHandle* vh : m_d->vertices)
569 messageVerbose("rerandomiseRandomVertexColours end");
570}
#define AmgSymMatrix(dim)
static Double_t a
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
void updateGUI()
void message(const QString &) const
AUTO - An Undocumented Tracking Object.
Definition LinkToTrack.h:20
const Amg::Vector3D & position() const
return position of vertex
Definition Vertex.cxx:63
std::vector< Trk::VxTrackAtVertex * > * vxTrackAtVertex(void)
Unconst pointer to the vector of tracks Required by some of the vertex fitters.
const Trk::RecVertex & recVertex(void) const
Returns a reference to reconstructed vertex.
virtual MsgStream & dump(MsgStream &sl) const
Output Method for MsgStream, to be overloaded by child classes.
static VP1CameraHelper * animatedZoomToPath(SoCamera *camera, SoGroup *sceneroot, SoPath *path, double duration_in_secs=1.0, double clipVolPercent=100.0, double slack=1.0, const SbVec3f &lookat=SbVec3f(999, 999, 999), const SbVec3f &upvec=SbVec3f(999, 999, 999), bool varySpeed=true, bool forceCircular=false)
void messageVerbose(const QString &) const
void message(const QString &) const
IVP1System * systemBase() const
static void setMaterialParameters(SoMaterial *m, const QColor &, const double &brightness=0.0, const double &transp=0.0)
static bool verbose()
Definition VP1Msg.h:31
QStringList getKeys() const
SoMaterial * material() const
virtual void largeChangesEnd()
virtual void init(VP1MaterialButtonBase *button=0)
SoSeparator * collSep() const
All 3D objects from this coll.
virtual void largeChangesBegin()
void visibilityChanged(bool)
QString text() const
VP1StdCollection(IVP1System *, const QString &helperClassName)
void updateShapes(VertexSysController *controller)
VertexCommonFlags::ReconVertexTypeFlag type()
const Trk::VxCandidate * vertex() const
void actualBuildShape(VertexSysController *controller)
void ensureAttached(SoSeparator *collSep, VertexSysController *controller)
VertexHandle(const Trk::VxCandidate *v, VP1VertexCollection *collHandle)
static QString comboBoxEntry_ColourByRandom()
void recheckCut(VertexHandle *vh)
bool cut(VertexHandle *vh, VertexSysController *controller)
void findAssociatedTracks(const Trk::VxCandidate *vtx, QList< const Trk::Track * > &tracks, QList< const Trk::TrackParticleBase * > &trackparticles)
VertexSysController * controller
VP1VertexCollection * theclass
static QString comboBoxEntry_ColourByCollection()
QList< VertexHandle * > vertices
VP1VertexCollection(VertexSysController *controller, const QString &key)
void init(VP1MaterialButtonBase *mat=0)
QStringList infoOnClicked(SoPath *pickedPath)
void collVisibilityChanged(bool vis)
COLOURBY colourBy() const
virtual COLOURBY defaultColourBy() const
QList< QWidget * > provideExtraWidgetsForGuiRow() const
void assignDefaultMaterial(SoMaterial *) const
void tracksFromVertexChanged(QList< std::pair< const SoMaterial *, QList< const Trk::Track * > > > &)
static QList< VP1StdCollection * > createCollections(VertexSysController *controller)
int r
Definition globals.cxx:22
Eigen::Matrix< double, 3, 1 > Vector3D
@ V0LambdaBar
Temporary addition for V0 LambdaBar.
Definition VertexType.h:34
@ V0Vtx
Vertex from V0 Decay.
Definition VertexType.h:31
@ KinkVtx
Kink Vertex.
Definition VertexType.h:32
@ ConvVtx
Converstion Vertex.
Definition VertexType.h:30
@ PileUp
Pile Up Vertex.
Definition VertexType.h:29
@ SecVtx
Secondary Vertex.
Definition VertexType.h:28
@ PriVtx
Primary Vertex.
Definition VertexType.h:27
@ V0KShort
Temporary addition for KShort.
Definition VertexType.h:35
@ V0Lambda
Temporary addition for V0 Lambda.
Definition VertexType.h:33