HepMC3 event record library
test
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
"
12
#include "
HepMC3/ReaderAsciiHepMC2.h
"
13
#include "
HepMC3/WriterAsciiHepMC2.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
}
GenEvent.h
Definition of class GenEvent.
HepMC3::GenEvent::event_number
int event_number() const
Get event number.
Definition:
GenEvent.h:135
HepMC3::WriterAscii
GenEvent I/O serialization for structured text files.
Definition:
WriterAscii.h:25
HepMC3::GenEvent
Stores event-related information.
Definition:
GenEvent.h:41
GenVertex.h
Definition of class GenVertex.
HepMC3
HepMC3 main namespace.
Definition:
AnalysisExample.h:19
GenParticle.h
Definition of class GenParticle.
ReaderAsciiHepMC2.h
Definition of class ReaderAsciiHepMC2.
WriterAscii.h
Definition of class WriterAscii.
WriterAsciiHepMC2.h
Definition of class WriterAsciiHepMC2.
HepMC3::GenCrossSection::xsec
double xsec(const std::string &wName) const
Get the cross section corresponding to the weight named wName.
Definition:
GenCrossSection.h:122
HepMC3::GenCrossSection
Stores additional information about cross-section.
Definition:
GenCrossSection.h:42
HepMC3::ReaderAsciiHepMC2
Parser for HepMC2 I/O files.
Definition:
ReaderAsciiHepMC2.h:30
ReaderAscii.h
Definition of class ReaderAscii.
main
int main(int argc, char **argv)
Definition:
rootIOTree_example_read.cc:23
Attribute.h
Definition of class Attribute, class IntAttribute and class StringAttribute.
HepMC3::GenEvent::attribute
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition:
GenEvent.h:389
Generated on Tue Apr 6 2021 00:00:00 for HepMC3 event record library by
1.8.20