ATLAS Offline Software
VP1MCSystem.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 #include "HepPDT/ParticleDataTable.hh"
6 #include "HepPDT/ParticleData.hh"
8 #include "ui_vp1mccontroller.h"
10 #include "VP1Base/VP1Serialise.h"
11 #include "VP1Base/VP1Deserialise.h"
13 
14 #include "GaudiKernel/IClassIDSvc.h"
15 #include "GaudiKernel/ISvcLocator.h"
16 #include <QListWidget>
17 #include <QListWidgetItem>
18 #include <QTreeWidget>
19 #include <QTreeWidgetItem>
20 
21 #include "AtlasHepMC/GenVertex.h"
22 #include "AtlasHepMC/GenParticle.h"
23 #include "CLHEP/Vector/ThreeVector.h"
25 #include "GaudiKernel/IPartPropSvc.h"
27 #include "CLHEP/Units/SystemOfUnits.h"
28 #include <cmath>
29 #include <stack>
30 
31 //____________________________________________________________________
33 public:
34  Imp(VP1MCSystem*tc);
36  Ui::VP1MCSystemControllerForm ui{};
37  QTreeWidget *tw;
38  IPartPropSvc *pps;
39 
40  void handle(QTreeWidgetItem *item, const HepMC::GenParticle &particle);
41 
42  void expand(const QString& text, QTreeWidgetItem *item);
43  void zeroFormat(QTreeWidgetItem *item);
44 };
45 
46 VP1MCSystem::Imp::Imp(VP1MCSystem *tc):theclass(tc),tw(0),pps(0) {
47 
48  ISvcLocator* svcLoc = theclass->serviceLocator();
49  if (!svcLoc) {
50  theclass->message("Error: Got NULL pointer to the service locator!");
51  return;
52  }
53  StatusCode status = svcLoc->service( "PartPropSvc",pps );
54  if(!status.isSuccess()) {
55  pps=0;
56  theclass->message("Error: Could not retrieve PartPropSvc!!");
57  return;
58  }
59 
60 }
61 
62 void VP1MCSystem::Imp::expand(const QString& name, QTreeWidgetItem *item) {
63  QFont font = item->font(0);
64 
65  if (item->text(0)==name) {
66  font.setBold(true);
67  for (int i=0;i<item->columnCount();++i) {
68  item->setFont(i,font);
69  }
70 
71 
72  QTreeWidgetItem *expandItem = item;
73  while (expandItem) {
74  expandItem->setExpanded(true);
75  expandItem=expandItem->parent();
76  if (!expandItem || expandItem->isExpanded()) break;
77  }
78  }
79  for (int i=0;i<item->childCount();++i) {
80  expand(name, item->child(i));
81  }
82 }
83 
84 void VP1MCSystem::Imp::zeroFormat(QTreeWidgetItem *item) {
85  QFont font = item->font(0);
86  font.setBold(false);
87  for (int i=0;i<item->columnCount();++i) {
88  item->setFont(i,font);
89  }
90  for (int i=0;i<item->childCount();++i) {
91  zeroFormat(item->child(i));
92  }
93 }
94 
95 void VP1MCSystem::Imp::handle(QTreeWidgetItem *item, const HepMC::GenParticle &theParticle) {
96 
97 #ifdef PD
98  const HepPDT::ParticleDataTable * dataTable = pps->PDT();
99  const HepPDT::ParticleData * particleData = dataTable->particle(iabs(theParticle.pdg_id()));
100  // Generate text
101  if (particleData) {
102  }
103  else {
104  }
105 #endif
106 
107  std::ostringstream partStream, pidStream, massStream, ptStream, etaStream, phiStream;
108 
109  bool ok;
110  QString name = VP1ParticleData::particleName(theParticle.pdg_id(),ok);
111  if (ok) {
112  partStream << name.toStdString();
113  }
114  else {
115  partStream << "PDG ID = " << theParticle.pdg_id();
116  }
117 
118 
119  item->setText(0,partStream.str().c_str());
120 
121  double eta = 0;
122  CLHEP::Hep3Vector mom=CLHEP::Hep3Vector(theParticle.momentum().x(),
123  theParticle.momentum().y(),
124  theParticle.momentum().z());
125  if (mom.x()!=0 || mom.y()!=0) {
126  eta =-log(tan(mom.theta()/2));
127  }
128  double phi = mom.phi();
129  while (phi<0) phi += 2*M_PI;
130  while (phi>2*M_PI) phi -= 2*M_PI;
131  double pt = mom.perp()/CLHEP::GeV;
132 
133  pidStream << theParticle.pdg_id();
134  item->setText(1,pidStream.str().c_str());
135 
136  massStream << theParticle.generated_mass()/CLHEP::GeV;
137  item->setText(2,massStream.str().c_str());
138 
139  ptStream << pt;
140  item->setText(3,ptStream.str().c_str());
141 
142  etaStream << eta;
143  item->setText(4,etaStream.str().c_str());
144 
145  phiStream << phi;
146  item->setText(5,phiStream.str().c_str());
147 
148  if (1) {
149  HepMC::ConstGenVertexPtr prodVertex = theParticle.production_vertex();
150 
151  QBrush brush=item->foreground(0);
152  brush.setColor(Qt::gray);
153 
154  if (prodVertex) {
155  int nParents=prodVertex->particles_in_size();
156  if (nParents==0) {
157  brush.setColor(Qt::black);
158  }
159  else if (nParents==1){
160  brush.setColor(Qt::blue);
161  }
162  else if (nParents>1) {
163  brush.setColor(Qt::red);
164  }
165  }
166 
167  item->setForeground(0,brush);
168  item->setForeground(1,brush);
169  item->setForeground(2,brush);
170  item->setForeground(3,brush);
171  item->setForeground(4,brush);
172  }
173  if (1) {
174  HepMC::ConstGenVertexPtr decayVertex = theParticle.end_vertex();
175  if (decayVertex) {
176  // decayVertex->print();
177  for ( const HepMC::ConstGenParticlePtr& current : *decayVertex ) {
178  QTreeWidgetItem *newItem = new QTreeWidgetItem();
179  item->addChild(newItem);
180  handle(newItem,*current);
181  }
182  }
183  }
184 }
185 
186 
187 //____________________________________________________________________
189  : IVP1System("MC",
190  "System for browsing the MC Event",
191  "Joe Boudreau <boudreau@pitt.edu> (original), Thomas.Kittelmann@cern.ch (VP1 implementation)"),
192  m_d(new Imp(this))
193 {
194 }
195 
196 
197 //____________________________________________________________________
199 {
200  delete m_d; m_d=0;
201 }
202 
203 
204 
205 //____________________________________________________________________
206 void VP1MCSystem::create( StoreGateSvc* /*detstore*/ )
207 {
208  QWidget * controller = new QWidget;
209  m_d->ui.setupUi(controller);
210  connect(m_d->ui.addButton,SIGNAL(clicked()), this, SLOT(addParticle()));
211  connect(m_d->ui.subtractButton,SIGNAL(clicked()), this, SLOT(removeParticle()));
212  connect(m_d->ui.searchButton,SIGNAL(clicked()), this, SLOT(searchParticles()));
213  connect (m_d->ui.listWidget, SIGNAL (itemDoubleClicked (QListWidgetItem *)), this, SLOT (editItem (QListWidgetItem *)));
214  registerController(controller);
215 
216 }
217 
218 //____________________________________________________________________
220 {
221  //erase();
223  if (VP1SGAccessHelper(this).retrieve(iter,endColl)) {
224  for (;iter!=endColl;++iter) {
226  for (e=iter->begin();e!=iter->end(); ++e) {
227  for (const HepMC::ConstGenParticlePtr& particle : **e) {
228  if (!particle->production_vertex() || ! particle->production_vertex()->particles_in_size()) {
229  QTreeWidgetItem *item = new QTreeWidgetItem();
230  m_d->tw->insertTopLevelItem(m_d->tw->topLevelItemCount(), item);
231  m_d->handle(item,*particle);
232  }
233  }
234  }
235  }
236  }
237 }
238 
239 
240 //____________________________________________________________________
242 {
243  m_d->tw->clear();
244 }
245 
246 //____________________________________________________________________
248 {
249  VP1Serialise serialise(0/*version*/,this);
250  serialise.save(IVP1System::saveState());//Info from base class
251  serialise.save(m_d->ui.listWidget->count());
252  for (int i=0;i<m_d->ui.listWidget->count();++i) {
253  m_d->ui.listWidget->setCurrentRow(i);
254  serialise.save(m_d->ui.listWidget->item(i)->text());
255  }
256  serialise.warnUnsaved(controllerWidget());
257  return serialise.result();
258 
259 }
260 
261 //____________________________________________________________________
262 void VP1MCSystem::restoreFromState(QByteArray ba)
263 {
264  VP1Deserialise state(ba,this);
265  if (state.version()!=0) {
266  message("Warning: State data in .vp1 file is in wrong format - ignoring!");
267  return;
268  }
269  IVP1System::restoreFromState(state.restoreByteArray());
270  qint32 itemCount=state.restoreInt();
271  for (int i=0;i<itemCount;++i) {
272  QString text=state.restoreString();
273  m_d->ui.listWidget->addItem(text);
274  }
275  state.warnUnrestored(controllerWidget());
276 }
277 
278 void VP1MCSystem::setTree(QTreeWidget *tw) {
279  m_d->tw=tw;
280  m_d->tw->setColumnCount(5);
281  m_d->tw->setHeaderLabels((QStringList()<<"Type"<< "ID" << "Mass (GeV) " << "Pt (GeV)"<<"Eta"<<"Phi"));
282  m_d->tw->setAlternatingRowColors ( true );
283  m_d->tw->setEditTriggers(QAbstractItemView::NoEditTriggers);
284 }
286  message("Adding...");
287  m_d->ui.listWidget->addItem("<particleName>");
288  m_d->ui.listWidget->setCurrentRow(m_d->ui.listWidget->count()-1);
289  //QListWidgetItem *item =m_d->ui.listWidget->currentItem();
290  //item->setFlags(Qt::ItemIsEditable|Qt::ItemIsSelectable);
291  //m_d->ui.listWidget->editItem(item);
292 }
294  message("Removing...");
295  QList<QListWidgetItem *> selectedItems =m_d->ui.listWidget->selectedItems();
296  for (int i=0;i<selectedItems.size();++i) {
297  int row = m_d->ui.listWidget->row(selectedItems[i]);
298  QListWidgetItem *item = m_d->ui.listWidget->takeItem(row);
299  delete item;
300  }
301 }
303  message("Searching...");
304  m_d->tw->collapseAll();
305  for (int j=0;j<m_d->ui.listWidget->count();++j) {
306  m_d->ui.listWidget->setCurrentRow(j);
307  QString text = m_d->ui.listWidget->currentItem()->text();
308  for (int i=0;i<m_d->tw->topLevelItemCount();++i) {
309  m_d->zeroFormat(m_d->tw->topLevelItem(i));
310  }
311  }
312 
313  for (int j=0;j<m_d->ui.listWidget->count();++j) {
314  m_d->ui.listWidget->setCurrentRow(j);
315  QString text = m_d->ui.listWidget->currentItem()->text();
316  for (int i=0;i<m_d->tw->topLevelItemCount();++i) {
317  m_d->expand(text,m_d->tw->topLevelItem(i));
318  }
319  }
320 
321 
322 }
323 
324 void VP1MCSystem::editItem(QListWidgetItem *item) {
325  m_d->ui.listWidget->openPersistentEditor(item);
326 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
VP1Serialise.h
query_example.row
row
Definition: query_example.py:24
fillPileUpNoiseLumi.current
current
Definition: fillPileUpNoiseLumi.py:52
VP1MCSystem::restoreFromState
void restoreFromState(QByteArray)
Definition: VP1MCSystem.cxx:262
VP1MCSystem::Imp::pps
IPartPropSvc * pps
Definition: VP1MCSystem.cxx:38
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
VP1MCSystem::setTree
void setTree(QTreeWidget *tw)
Definition: VP1MCSystem.cxx:278
VP1Deserialise.h
VP1Serialise
Definition: VP1Serialise.h:45
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:64
RoiUtil::serialise
void serialise(const std::vector< const IRoiDescriptor * > &rois, roiserial_type &s)
serialise an entire vector of IRoiDescriptors
Definition: RoiSerialise.cxx:45
VP1MCSystem::addParticle
void addParticle()
Definition: VP1MCSystem.cxx:285
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:79
VP1MCSystem::editItem
void editItem(QListWidgetItem *)
Definition: VP1MCSystem.cxx:324
GenVertex.h
fillPileUpNoiseLumi.connect
string connect
Definition: fillPileUpNoiseLumi.py:70
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
VP1MCSystem::Imp::ui
Ui::VP1MCSystemControllerForm ui
Definition: VP1MCSystem.cxx:36
test_pyathena.pt
pt
Definition: test_pyathena.py:11
M_PI
#define M_PI
Definition: ActiveFraction.h:11
VP1ParticleData.h
VP1MCSystem::erase
void erase()
Definition: VP1MCSystem.cxx:241
GenParticle.h
iabs
int iabs(int a)
Definition: SbPolyhedron.cxx:22
VP1MCSystem::refresh
void refresh(StoreGateSvc *storegate)
Definition: VP1MCSystem.cxx:219
IVP1System::state
State state() const
Definition: IVP1System.cxx:129
IVP1System::saveState
virtual QByteArray saveState()
Definition: IVP1System.cxx:294
VP1MCSystem::Imp::zeroFormat
void zeroFormat(QTreeWidgetItem *item)
Definition: VP1MCSystem.cxx:84
PyPoolBrowser.item
item
Definition: PyPoolBrowser.py:129
VP1MCSystem::searchParticles
void searchParticles()
Definition: VP1MCSystem.cxx:302
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:128
IVP1System
Definition: IVP1System.h:36
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
VP1MCSystem::Imp::theclass
VP1MCSystem * theclass
Definition: VP1MCSystem.cxx:35
McEventCollection.h
python.SystemOfUnits.gray
int gray
Definition: SystemOfUnits.py:257
lumiFormat.i
int i
Definition: lumiFormat.py:92
VP1MCSystem.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
python.MuonSegmentFindingConfig.pps
pps
Definition: MuonSegmentFindingConfig.py:626
IVP1System::serviceLocator
ISvcLocator * serviceLocator() const
Definition: IVP1System.cxx:324
IVP1System::name
const QString & name() const
Definition: IVP1System.cxx:50
VP1MCSystem::saveState
QByteArray saveState()
Definition: VP1MCSystem.cxx:247
drawFromPickle.tan
tan
Definition: drawFromPickle.py:36
VP1MCSystem::~VP1MCSystem
virtual ~VP1MCSystem()
Definition: VP1MCSystem.cxx:198
VP1Deserialise
Definition: VP1Deserialise.h:44
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
VP1MCSystem::removeParticle
void removeParticle()
Definition: VP1MCSystem.cxx:293
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
VP1MCSystem::Imp::tw
QTreeWidget * tw
Definition: VP1MCSystem.cxx:37
item
Definition: ItemListSvc.h:43
IVP1System::registerController
void registerController(QWidget *)
Definition: IVP1System.cxx:224
VP1MCSystem::m_d
Imp * m_d
Definition: VP1MCSystem.h:50
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
PyPoolBrowser.expand
expand
Definition: PyPoolBrowser.py:128
VP1MCSystem::Imp::Imp
Imp(VP1MCSystem *tc)
Definition: VP1MCSystem.cxx:46
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
makeTransCanvas.text
text
Definition: makeTransCanvas.py:11
VP1MCSystem::Imp::handle
void handle(QTreeWidgetItem *item, const HepMC::GenParticle &particle)
Definition: VP1MCSystem.cxx:95
plotBeamSpotMon.font
font
Definition: plotBeamSpotMon.py:80
merge.status
status
Definition: merge.py:17
HepMC::ConstGenVertexPtr
const HepMC::GenVertex * ConstGenVertexPtr
Definition: GenVertex.h:60
VP1SGAccessHelper
Definition: VP1SGAccessHelper.h:25
VP1MCSystem::Imp
Definition: VP1MCSystem.cxx:32
VP1MCSystem::create
void create(StoreGateSvc *detstore)
Definition: VP1MCSystem.cxx:206
VP1MCSystem::Imp::expand
void expand(const QString &text, QTreeWidgetItem *item)
Definition: VP1MCSystem.cxx:62
VP1MCSystem::VP1MCSystem
VP1MCSystem()
Definition: VP1MCSystem.cxx:188
VP1MCSystem
Definition: VP1MCSystem.h:25
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
red
@ red
Definition: BinsDiffFromStripMedian.h:18
VP1SGAccessHelper.h
SG::ConstIterator
Definition: SGIterator.h:163
IVP1System::message
void message(const QString &) const
Definition: IVP1System.cxx:336
TrackingGeometryValidation.blue
blue
Definition: TrackingGeometryValidation.py:33
VP1ParticleData::particleName
static QString particleName(const int &pdgcode, bool &ok)
Definition: VP1ParticleData.cxx:128
GenParticle
@ GenParticle
Definition: TruthClasses.h:30
VP1SGContentsHelper.h
IVP1System::controllerWidget
QWidget * controllerWidget()
Definition: IVP1System.cxx:202
IVP1System::restoreFromState
virtual void restoreFromState(QByteArray)
Definition: IVP1System.cxx:302