HepMC3 event record library
testDelete2.cc
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5 //
6 #include "HepMC3/Print.h"
7 #include "HepMC3/GenEvent.h"
8 #include "HepMC3/GenParticle.h"
9 #include "HepMC3/GenVertex.h"
10 #include "HepMC3/ReaderAscii.h"
11 #include "HepMC3/WriterAscii.h"
14 #include "HepMC3TestUtils.h"
15 using namespace HepMC3;
16 int main()
17 {
18  ReaderAsciiHepMC2 inputA("inputDelete2.hepmc");
19  if(inputA.failed()) return 1;
20  std::vector<std::shared_ptr<GenEvent> > evts;
21  while( !inputA.failed() )
22  {
23  std::shared_ptr<GenEvent> evt= std::make_shared<GenEvent>();
24  inputA.read_event(*evt);
25  if( inputA.failed() ) {
26  printf("End of file reached. Exit.\n");
27  break;
28  }
29  evts.push_back(evt);
30  }
31  inputA.close();
32  int i=0;
33  int j=0;
34  while(i==j)
35  {
36  i=rand()% evts.size();
37  j=rand()% evts.size();
38  }
39 
40  std::vector<GenVertexPtr> selectedVtx;
41  for (std::vector<GenParticlePtr>::const_iterator p=evts.at(i)->particles().begin(); p!=evts.at(i)->particles().end(); ++p)
42  for (std::vector<GenVertexPtr>::iterator v=evts.at(j)->vertices().begin(); v!=evts.at(j)->vertices().end(); ++v)
43  {
44  selectedVtx.push_back(*v);
45  }
46 
47  WriterAscii outputA("frominputDelete2.hepmc");
48  if(outputA.failed()) return 2;
49  for (size_t i=0; i<evts.size(); i++) outputA.write_event(*evts[i]);
50  evts.clear();
51  outputA.close();
52  for (std::vector<GenVertexPtr>::const_iterator v=selectedVtx.begin(); v!=selectedVtx.end(); ++v)
53  {
54  Print::line(*v);
55  }
56  if (selectedVtx.front()->parent_event())
57  Print::listing(*(selectedVtx.front()->parent_event()));
58  ReaderAscii inputB("frominputDelete2.hepmc");
59  if(inputB.failed()) return 3;
60  WriterAsciiHepMC2 outputB("fromfrominputDelete2.hepmc");
61  if(outputB.failed()) return 4;
62  while( !inputB.failed() )
63  {
64  GenEvent evt(Units::GEV,Units::MM);
65  inputB.read_event(evt);
66  if( inputB.failed() ) {
67  printf("End of file reached. Exit.\n");
68  break;
69  }
70  outputB.write_event(evt);
71  evt.clear();
72  }
73  inputB.close();
74  outputB.close();
75  return COMPARE_ASCII_FILES("fromfrominputDelete2.hepmc","inputDelete2.hepmc");
76 }
GenEvent I/O serialization for structured text files.
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:29
Definition of class GenParticle.
Definition of class GenVertex.
Definition of class WriterAscii.
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:50
Parser for HepMC2 I/O files.
Definition of class ReaderAsciiHepMC2.
Stores event-related information.
Definition: GenEvent.h:41
Definition of class ReaderAscii.
static void line(std::ostream &os, const GenEvent &event, bool attributes=false)
Print one-line info.
Definition: Print.cc:202
Definition of class WriterAsciiHepMC2.
int main(int argc, char **argv)
Definition of class GenEvent.
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25