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#ifdef HEPMC3
119 for (const auto& PartIn: m_vertex->particles_in()){
120#else
121 HepMC::GenVertex::particles_in_const_iterator itPartIn,itPartInE(m_vertex->particles_in_const_end());
122 for ( itPartIn = m_vertex->particles_in_const_begin();itPartIn!=itPartInE;++itPartIn) {
123 auto PartIn=*itPartIn;
124#endif
125 px +=PartIn->momentum().px();
126 py +=PartIn->momentum().py();
127 pz +=PartIn->momentum().pz();
128 e += PartIn->momentum().e();
129 }
130 const HepMC::FourVector mom(px,py,pz,e);
131 switch(q) {
132 case VertexCommonFlags::ENERGY: return e;
133 case VertexCommonFlags::MOMENTUM: return m_d->mag(mom);
134 case VertexCommonFlags::TRANSVERSE_MOM: return mom.perp();
135 default: // VertexCommonFlags::MASS:
136 return mom.m();
137 }
138 }
139 private:
140 void ensureAttach(SoSeparator * collsep) {
141 if (m_attached)
142 return;
143 m_attached = true;
144 if (!m_line) {
145 m_line = new SoLineSet;
146 m_line->ref();
147 }
148 updateShape();
149 collsep->addChild(m_line);
150 }
151 void ensureDetach(SoSeparator * collsep) {
152 if (!m_attached)
153 return;
154 m_attached = false;
155 collsep->removeChild(m_line);
156 }
157 bool cut() {
158
159 if (m_d->controller->truthCutPrimaryVertexOnly()&&this!=m_d->vertices.at(0))
160 return false;
161
162 if (!m_d->controller->truthCutAllowedEta().contains(m_vertex->position().eta()))
163 return false;
164
165 QPair<VertexCommonFlags::QUANTITY,VP1Interval> p = m_d->controller->truthCutQuantity();
166 if (!p.second.isAllR()) {
167 if (!p.second.contains(quantityVal(p.first)))
168 return false;
169 }
170
171 //We handle phi cut last:
172 double phi(m_vertex->position().phi());
173 for (const VP1Interval& i : m_d->controller->truthCutAllowedPhi()) {
174 if (i.contains(phi)||i.contains(phi+2*M_PI)||i.contains(phi-2*M_PI))
175 return true;
176 }
177 return false;
178 }
181 SoLineSet * m_line;
183 };
184
185 QList<VertexHandle*> vertices;
186};
187
188
189//____________________________________________________________________
191 : VP1StdCollection(controller->systemBase(),"VP1TruthVertexCollection_"+key), m_d(new Imp)
192{
193 m_d->key = key;
194 m_d->theclass = this;
195 m_d->controller = controller;
196 connect(controller,SIGNAL(truthCutAllowedEtaChanged(const VP1Interval&)),this,SLOT(recheckAllCuts()));
197 connect(controller,SIGNAL(truthCutAllowedPhiChanged(const QList<VP1Interval>&)),this,SLOT(recheckAllCuts()));
198 connect(controller,SIGNAL(truthCutPrimaryVertexOnlyChanged(bool)),this,SLOT(recheckAllCuts()));
199 connect(controller,SIGNAL(truthCutQuantityChanged(const QPair<VertexCommonFlags::QUANTITY,VP1Interval>&)),this,SLOT(recheckAllCuts()));
200 connect(controller,SIGNAL(truthCrossLengthChanged(QPair<VertexCommonFlags::QUANTITY,double>)),this,SLOT(updateAllShapes()));
201}
202
203//____________________________________________________________________
205{
206 for (Imp::VertexHandle*vh : m_d->vertices)
207 delete vh;
208 delete m_d;
209}
210
211//____________________________________________________________________
213{
214 return m_d->key;
215}
216
217//____________________________________________________________________
219{
220 VP1MaterialButton::setMaterialParameters( m, 0.66667/*red*/, 0.66667/*green*/, 1.0/*blue*/, 0.15 /*brightness*/ );
221}
222
223//_____________________________________________________________________________________
224SoLineSet * VP1TruthVertexCollection::Imp::createCross(const double& x, const double& y, const double& z, const double& extent )
225{
226 SoVertexProperty *vertices = new SoVertexProperty();
227 vertices->vertex.set1Value ( 0,x-extent, y, z );
228 vertices->vertex.set1Value ( 1,x+extent, y,z );
229 vertices->vertex.set1Value ( 2, x,y-extent, z);
230 vertices->vertex.set1Value ( 3, x,y+extent, z );
231 vertices->vertex.set1Value ( 4, x, y,z-extent );
232 vertices->vertex.set1Value ( 5, x, y,z+extent );
233 SoLineSet * line = new SoLineSet();
234 line->numVertices.set1Value(0,2);
235 line->numVertices.set1Value(1,2);
236 line->numVertices.set1Value(2,2);
237 line->vertexProperty = vertices;
238 return line;
239}
240
241//____________________________________________________________________
243{
244 const McEventCollection* mcEventColl;
245 if (!VP1SGAccessHelper(systemBase()).retrieve(mcEventColl, m_d->key))
246 return false;
247
248 //Fixme: Here we take the first event in the event collection. Instead we should loop!
249 if (mcEventColl->size()<1)//Fixme
250 return false;
251
252 McEventCollection::const_iterator itEvent, itEventEnd(mcEventColl->end());
253 for (itEvent = mcEventColl->begin(); itEvent != itEventEnd; ++itEvent) {
254 const HepMC::GenEvent* genEvent(*itEvent);
255 if (!genEvent)
256 continue;
257
258#ifdef HEPMC3
259 for (const auto& vtx: genEvent->vertices()) {
260#else
261 HepMC::GenEvent::vertex_const_iterator itVertex, itVertexEnd(genEvent->vertices_end());
262 for (itVertex = genEvent->vertices_begin(); itVertex != itVertexEnd; ++itVertex ) {
263 auto vtx=*itVertex;
264#endif
265 if (!vtx)
266 continue;
267 m_d->vertices << new Imp::VertexHandle(vtx,m_d);
268 }
269 }
270
272
273 return true;
274}
275
276//____________________________________________________________________
277QStringList VP1TruthVertexCollection::infoOnClicked(SoPath* pickedPath)
278{
279 //Get HepMC::GenVertex pointer associated with the clicked node:
280 SoNode * pickedNode = (pickedPath ? (pickedPath->getLength()>0?pickedPath->getNodeFromTail(0):0): 0);
281
282 Imp::VertexHandle* vertexHandle(0);
283 for (Imp::VertexHandle* vh : m_d->vertices) {
284 if (vh->line()==pickedNode) {
285 vertexHandle = vh;
286 break;
287 }
288 }
289 if (!vertexHandle)
290 return QStringList() << "ERROR: Could not get truth vertex information for picked Node";
291 HepMC::ConstGenVertexPtr vtx = vertexHandle->vertex();
292
293 QStringList l;
294 if (m_d->controller->printInfoOnClick()) {
295
296 //Make output:
297 l <<"Truth vertex from collection "+text()+":" ;
298#ifdef HEPMC3
299 for (const auto& PartIn: vtx->particles_in()) {
300#else
301 HepMC::GenVertex::particles_in_const_iterator itPartIn,itPartInE(vtx->particles_in_const_end());
302 for ( itPartIn = vtx->particles_in_const_begin();itPartIn!=itPartInE;++itPartIn) {
303 auto PartIn=*itPartIn;
304#endif
305 const int pdg = PartIn->pdg_id();
306 bool ok;
307 QString name = VP1ParticleData::particleName(pdg,ok);
308 if (!ok)
309 name = "<unknown>";
310 l << "--> In: "+name+" ("+str(pdg)+") [ P = "+str(m_d->mag(PartIn->momentum())/Gaudi::Units::GeV)+" GeV ]";
311 }
312#ifdef HEPMC3
313 for (const auto& PartOut: vtx->particles_out()) {
314#else
315 HepMC::GenVertex::particles_out_const_iterator itPartOut,itPartOutE(vtx->particles_out_const_end());
316 for ( itPartOut = vtx->particles_out_const_begin();itPartOut!=itPartOutE;++itPartOut) {
317 auto PartOut=*itPartOut;
318#endif
319 const int pdg = PartOut->pdg_id();
320 bool ok;
321 QString name = VP1ParticleData::particleName(pdg,ok);
322 if (!ok)
323 name = "<unknown>";
324 l << "--> Out: "+name+" ("+str(pdg)+") [ P = "+str(m_d->mag(PartOut->momentum())/Gaudi::Units::GeV)+" GeV ]";
325 }
326
327 if (m_d->controller->printVerboseInfoOnClick()) {
328 l <<"======== Dump ========";
329 std::ostringstream s;
330 HepMC::Print::line(s,vtx);
331 l << QString(s.str().c_str()).split('\n');
332 l <<"======================";
333 }
334
335 }
336 if (m_d->controller->zoomOnClick()) {
337 std::set<SoCamera*> cameras = static_cast<IVP13DSystem*>(systemBase())->getCameraList();
338 std::set<SoCamera*>::iterator it,itE = cameras.end();
339 for (it=cameras.begin();it!=itE;++it)
340 VP1CameraHelper::animatedZoomToPath(*it,collSep(),pickedPath,2.0,1.0);
341 }
342
343 return l;
344}
345
346//____________________________________________________________________
348{
349 static_cast<IVP13DSystem*>(systemBase())->deselectAll();
351 for (Imp::VertexHandle* vh : m_d->vertices)
352 vh->recheckCutStatus();
354}
355
356//____________________________________________________________________
358{
360 for (Imp::VertexHandle* vh : m_d->vertices)
361 vh->updateShape();
363}
#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 *)
void line(std::ostream &os, const GenEvent &e)
Definition GenEvent.h:677
const HepMC::GenVertex * ConstGenVertexPtr
Definition GenVertex.h:60