HepMC3 event record library
testThreads1.cc
1 // -*- C++ -*-
2 #include "HepMC3/Attribute.h"
3 #include "HepMC3/GenEvent.h"
4 #include "HepMC3/GenParticle.h"
5 #include "HepMC3/GenVertex.h"
6 #include "HepMC3/ReaderAscii.h"
7 #include "HepMC3/WriterAscii.h"
10 #include "HepMC3TestUtils.h"
11 #include <thread>
12 using namespace HepMC3;
13 const int NinputCopies=4;
14 const int NmaxThreads=3;
15 void attribute_function1(const GenEvent& e, const int& id)
16 {
17  shared_ptr<GenCrossSection> xs = e.attribute<GenCrossSection>("GenCrossSection",0);
18  printf("XS in event %i is %f, id=%i\n",e.event_number(),xs->xsec(),id);
19 }
20 int main()
21 {
22  ReaderAsciiHepMC2 inputA("inputCommon.hepmc");
23  if(inputA.failed()) return 1;
24  std::vector<GenEvent> evts;
25  while( !inputA.failed() )
26  {
27  GenEvent evt=GenEvent(Units::GEV,Units::MM);
28  inputA.read_event(evt);
29  if( inputA.failed() ) {
30  printf("End of file reached. Exit.\n");
31  break;
32  }
33  evts.push_back(evt);
34  }
35  inputA.close();
36  std::vector<GenEvent> thr_evts[NinputCopies];
37  for (int i=0; i<NinputCopies; i++)thr_evts[i]=evts;
38 
39  for (int i=0; i<NinputCopies; i++)
40  for (int e=0; e<evts.size(); e++)
41  {
42  std::vector<std::thread> threads;
43  int j1=-thr_evts[i].at(e).vertices().size();
44  int j2=thr_evts[i].at(e).particles().size();
45  int d=(j2-j1)/NmaxThreads;
46  std::vector<int> ids;
47  ids.push_back(0);
48  for (int j=j1; j<j2; j+=d)
49  ids.push_back(j);
50  /* The arguments to the thread function are moved or copied by value.
51  If a reference argument needs to be passed to the thread function, it
52  has to be wrapped (e.g. with std::ref or std::cref).
53  */
54  for (int j=0; j<ids.size(); j++)
55  threads.push_back(std::thread(attribute_function1,std::cref(thr_evts[i].at(e)),ids[j]));
56  for (auto& th : threads) th.join();
57  threads.clear();
58  }
59  for (int k=0; k<NinputCopies; k++)
60  {
61  WriterAscii outputA("outputThreads1_"+std::to_string(k)+".hepmc");
62  if(outputA.failed()) return 2;
63  for (size_t i=0; i<thr_evts[k].size(); i++) outputA.write_event(thr_evts[k].at(i));
64  thr_evts[k].clear();
65  outputA.close();
66  if (k>0)
67  {
68  int result=COMPARE_ASCII_FILES("outputThreads1_"+std::to_string(k-1)+".hepmc","outputThreads1_"+std::to_string(k)+".hepmc");
69  if (result!=0) return result;
70  }
71  }
72  return 0;
73 }
Definition of class GenParticle.
Definition of class GenVertex.
Definition of class WriterAscii.
shared_ptr< T > attribute(const string &name, const int &id=0) const
Get attribute of type T.
Definition: GenEvent.h:381
int event_number() const
Get event number.
Definition: GenEvent.h:136
Parser for HepMC2 I/O files.
Definition of class ReaderAsciiHepMC2.
Stores event-related information.
Definition: GenEvent.h:42
Definition of class ReaderAscii.
Definition of class WriterAsciiHepMC2.
int main(int argc, char **argv)
Definition of class GenEvent.
Definition of class Attribute, class IntAttribute and class StringAttribute.
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25
Stores additional information about cross-section.