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