ATLAS Offline Software
Loading...
Searching...
No Matches
VP1TruthVertexCollection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7// //
8// Implementation of class VP1TruthVertexCollection //
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
26#include <Inventor/nodes/SoLineSet.h>
27#include <Inventor/nodes/SoVertexProperty.h>
28#include <Inventor/SoPath.h>
29#include <Inventor/nodes/SoSeparator.h>
30
31#include <QStringList>
32
34#include "AtlasHepMC/GenEvent.h"
36
37//#include "CLHEP/Units/SystemOfUnits.h"
38#include "GaudiKernel/SystemOfUnits.h"
39
40#include <sstream>
41
42//____________________________________________________________________
44{
45 QList<VP1StdCollection*> l;
46 for (const QString& key : VP1SGContentsHelper(controller->systemBase()).getKeys<McEventCollection>()) {
47 VP1TruthVertexCollection * col = new VP1TruthVertexCollection(controller,key);
48 col->init();
49 l << col;
50 }
51 return l;
52}
53
54
55//____________________________________________________________________
57public:
58 double mag(const HepMC::FourVector& v) const {
59 return std::sqrt( v.x()*v.x() + v.y()*v.y() + v.z()*v.z() );
60 }
61 QString key;
64 std::map <SoNode *, HepMC::ConstGenVertexPtr> nodeToVertexMap;
65 SoLineSet * createCross(const double& x, const double& y, const double& z, const double& extent = 10*Gaudi::Units::mm ); // 10*CLHEP::mm );
66
68 public:
70 ~VertexHandle() { if (m_line) m_line->unref(); }
71
73 bool cutval(cut());
74 if (cutval!=m_attached) {
75 if (cutval)
76 ensureAttach(m_d->theclass->collSep());
77 else
78 ensureDetach(m_d->theclass->collSep());
79 }
80 }
81 void updateShape() {
82 if (!m_line)
83 return;
84 if (!m_attached) {
85 //invalidate...
86 }
87
88 SoVertexProperty *vertices = static_cast<SoVertexProperty *>(m_line->vertexProperty.getValue());
89 if (!vertices) {
90 vertices = new SoVertexProperty();
91 m_line->numVertices.set1Value(0,2);
92 m_line->numVertices.set1Value(1,2);
93 m_line->numVertices.set1Value(2,2);
94 m_line->vertexProperty = vertices;
95 }
96
97 QPair<VertexCommonFlags::QUANTITY,double> p = m_d->controller->truthCrossLength();
98 double extent;
99 if (p.second<0)
100 extent = -p.second;
101 else
102 extent = p.second * quantityVal(p.first);
103
104 double x(m_vertex->position().x()), y(m_vertex->position().y()), z(m_vertex->position().z());
105 vertices->vertex.set1Value ( 0,x-extent, y, z );
106 vertices->vertex.set1Value ( 1,x+extent, y,z );
107 vertices->vertex.set1Value ( 2, x,y-extent, z);
108 vertices->vertex.set1Value ( 3, x,y+extent, z );
109 vertices->vertex.set1Value ( 4, x, y,z-extent );
110 vertices->vertex.set1Value ( 5, x, y,z+extent );
111 }
113 SoLineSet * line() const { return m_line; }
114
116 //Find total 4-momentum of incoming particles
117 double px(0), py(0), pz(0), e(0);//HepMC::FourVector does NOT support operators + or += !!!!!!
118 for (const auto& PartIn: m_vertex->particles_in()){
119 px +=PartIn->momentum().px();
120 py +=PartIn->momentum().py();
121 pz +=PartIn->momentum().pz();
122 e += PartIn->momentum().e();
123 }
124 const HepMC::FourVector mom(px,py,pz,e);
125 switch(q) {
126 case VertexCommonFlags::ENERGY: return e;
127 case VertexCommonFlags::MOMENTUM: return m_d->mag(mom);
128 case VertexCommonFlags::TRANSVERSE_MOM: return mom.perp();
129 default: // VertexCommonFlags::MASS:
130 return mom.m();
131 }
132 }
133 private:
134 void ensureAttach(SoSeparator * collsep) {
135 if (m_attached)
136 return;
137 m_attached = true;
138 if (!m_line) {
139 m_line = new SoLineSet;
140 m_line->ref();
141 }
142 updateShape();
143 collsep->addChild(m_line);
144 }
145 void ensureDetach(SoSeparator * collsep) {
146 if (!m_attached)
147 return;
148 m_attached = false;
149 collsep->removeChild(m_line);
150 }
151 bool cut() {
152
153 if (m_d->controller->truthCutPrimaryVertexOnly()&&this!=m_d->vertices.at(0))
154 return false;
155
156 if (!m_d->controller->truthCutAllowedEta().contains(m_vertex->position().eta()))
157 return false;
158
159 QPair<VertexCommonFlags::QUANTITY,VP1Interval> p = m_d->controller->truthCutQuantity();
160 if (!p.second.isAllR()) {
161 if (!p.second.contains(quantityVal(p.first)))
162 return false;
163 }
164
165 //We handle phi cut last:
166 double phi(m_vertex->position().phi());
167 for (const VP1Interval& i : m_d->controller->truthCutAllowedPhi()) {
168 if (i.contains(phi)||i.contains(phi+2*M_PI)||i.contains(phi-2*M_PI))
169 return true;
170 }
171 return false;
172 }
175 SoLineSet * m_line;
177 };
178
179 QList<VertexHandle*> vertices;
180};
181
182
183//____________________________________________________________________
185 : VP1StdCollection(controller->systemBase(),"VP1TruthVertexCollection_"+key), m_d(new Imp)
186{
187 m_d->key = key;
188 m_d->theclass = this;
189 m_d->controller = controller;
190 connect(controller,SIGNAL(truthCutAllowedEtaChanged(const VP1Interval&)),this,SLOT(recheckAllCuts()));
191 connect(controller,SIGNAL(truthCutAllowedPhiChanged(const QList<VP1Interval>&)),this,SLOT(recheckAllCuts()));
192 connect(controller,SIGNAL(truthCutPrimaryVertexOnlyChanged(bool)),this,SLOT(recheckAllCuts()));
193 connect(controller,SIGNAL(truthCutQuantityChanged(const QPair<VertexCommonFlags::QUANTITY,VP1Interval>&)),this,SLOT(recheckAllCuts()));
194 connect(controller,SIGNAL(truthCrossLengthChanged(QPair<VertexCommonFlags::QUANTITY,double>)),this,SLOT(updateAllShapes()));
195}
196
197//____________________________________________________________________
199{
200 for (Imp::VertexHandle*vh : m_d->vertices)
201 delete vh;
202 delete m_d;
203}
204
205//____________________________________________________________________
207{
208 return m_d->key;
209}
210
211//____________________________________________________________________
213{
214 VP1MaterialButton::setMaterialParameters( m, 0.66667/*red*/, 0.66667/*green*/, 1.0/*blue*/, 0.15 /*brightness*/ );
215}
216
217//_____________________________________________________________________________________
218SoLineSet * VP1TruthVertexCollection::Imp::createCross(const double& x, const double& y, const double& z, const double& extent )
219{
220 SoVertexProperty *vertices = new SoVertexProperty();
221 vertices->vertex.set1Value ( 0,x-extent, y, z );
222 vertices->vertex.set1Value ( 1,x+extent, y,z );
223 vertices->vertex.set1Value ( 2, x,y-extent, z);
224 vertices->vertex.set1Value ( 3, x,y+extent, z );
225 vertices->vertex.set1Value ( 4, x, y,z-extent );
226 vertices->vertex.set1Value ( 5, x, y,z+extent );
227 SoLineSet * line = new SoLineSet();
228 line->numVertices.set1Value(0,2);
229 line->numVertices.set1Value(1,2);
230 line->numVertices.set1Value(2,2);
231 line->vertexProperty = vertices;
232 return line;
233}
234
235//____________________________________________________________________
237{
238 const McEventCollection* mcEventColl;
239 if (!VP1SGAccessHelper(systemBase()).retrieve(mcEventColl, m_d->key))
240 return false;
241
242 //Fixme: Here we take the first event in the event collection. Instead we should loop!
243 if (mcEventColl->size()<1)//Fixme
244 return false;
245
246 McEventCollection::const_iterator itEvent, itEventEnd(mcEventColl->end());
247 for (itEvent = mcEventColl->begin(); itEvent != itEventEnd; ++itEvent) {
248 const HepMC::GenEvent* genEvent(*itEvent);
249 if (!genEvent)
250 continue;
251
252 for (const auto& vtx: genEvent->vertices()) {
253 if (!vtx)
254 continue;
255 m_d->vertices << new Imp::VertexHandle(vtx,m_d);
256 }
257 }
258
260
261 return true;
262}
263
264//____________________________________________________________________
265QStringList VP1TruthVertexCollection::infoOnClicked(SoPath* pickedPath)
266{
267 //Get HepMC::GenVertex pointer associated with the clicked node:
268 SoNode * pickedNode = (pickedPath ? (pickedPath->getLength()>0?pickedPath->getNodeFromTail(0):0): 0);
269
270 Imp::VertexHandle* vertexHandle(0);
271 for (Imp::VertexHandle* vh : m_d->vertices) {
272 if (vh->line()==pickedNode) {
273 vertexHandle = vh;
274 break;
275 }
276 }
277 if (!vertexHandle)
278 return QStringList() << "ERROR: Could not get truth vertex information for picked Node";
279 HepMC::ConstGenVertexPtr vtx = vertexHandle->vertex();
280
281 QStringList l;
282 if (m_d->controller->printInfoOnClick()) {
283
284 //Make output:
285 l <<"Truth vertex from collection "+text()+":" ;
286 for (const auto& PartIn: vtx->particles_in()) {
287 const int pdg = PartIn->pdg_id();
288 bool ok;
289 QString name = VP1ParticleData::particleName(pdg,ok);
290 if (!ok)
291 name = "<unknown>";
292 l << "--> In: "+name+" ("+str(pdg)+") [ P = "+str(m_d->mag(PartIn->momentum())/Gaudi::Units::GeV)+" GeV ]";
293 }
294 for (const auto& PartOut: vtx->particles_out()) {
295 const int pdg = PartOut->pdg_id();
296 bool ok;
297 QString name = VP1ParticleData::particleName(pdg,ok);
298 if (!ok)
299 name = "<unknown>";
300 l << "--> Out: "+name+" ("+str(pdg)+") [ P = "+str(m_d->mag(PartOut->momentum())/Gaudi::Units::GeV)+" GeV ]";
301 }
302
303 if (m_d->controller->printVerboseInfoOnClick()) {
304 l <<"======== Dump ========";
305 std::ostringstream s;
306 HepMC::Print::line(s,vtx);
307 l << QString(s.str().c_str()).split('\n');
308 l <<"======================";
309 }
310
311 }
312 if (m_d->controller->zoomOnClick()) {
313 std::set<SoCamera*> cameras = static_cast<IVP13DSystem*>(systemBase())->getCameraList();
314 std::set<SoCamera*>::iterator it,itE = cameras.end();
315 for (it=cameras.begin();it!=itE;++it)
316 VP1CameraHelper::animatedZoomToPath(*it,collSep(),pickedPath,2.0,1.0);
317 }
318
319 return l;
320}
321
322//____________________________________________________________________
324{
325 static_cast<IVP13DSystem*>(systemBase())->deselectAll();
327 for (Imp::VertexHandle* vh : m_d->vertices)
328 vh->recheckCutStatus();
330}
331
332//____________________________________________________________________
334{
336 for (Imp::VertexHandle* vh : m_d->vertices)
337 vh->updateShape();
339}
#define M_PI
Scalar phi() const
phi method
#define y
#define x
#define z
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.
size_type size() const noexcept
Returns the number of elements in the collection.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
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)
IVP1System * systemBase() const
static void setMaterialParameters(SoMaterial *m, const QColor &, const double &brightness=0.0, const double &transp=0.0)
static QString particleName(const int &pdgcode, bool &ok)
QStringList getKeys() const
virtual void largeChangesEnd()
virtual void init(VP1MaterialButtonBase *button=0)
SoSeparator * collSep() const
All 3D objects from this coll.
virtual void largeChangesBegin()
QString text() const
VP1StdCollection(IVP1System *, const QString &helperClassName)
double quantityVal(const VertexCommonFlags::QUANTITY &q)
VertexHandle(HepMC::ConstGenVertexPtr v, VP1TruthVertexCollection::Imp *dd)
SoLineSet * createCross(const double &x, const double &y, const double &z, const double &extent=10 *Gaudi::Units::mm)
std::map< SoNode *, HepMC::ConstGenVertexPtr > nodeToVertexMap
double mag(const HepMC::FourVector &v) const
void assignDefaultMaterial(SoMaterial *) const
VP1TruthVertexCollection(VertexSysController *, const QString &key)
QStringList infoOnClicked(SoPath *pickedPath)
static QList< VP1StdCollection * > createCollections(VertexSysController *)
HepMC3::FourVector FourVector
HepMC3::ConstGenVertexPtr ConstGenVertexPtr
Definition GenVertex.h:24
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39