HepMC3 event record library: HepMC3_fileIO_example.cc Source File

HepMC3 event record library

HepMC3_fileIO_example.cc
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6/**
7 * @example HepMC3_fileIO_example.cc
8 * @brief Test of file I/O
9 *
10 * Parses HepMC3 file and saves it as a new HepMC3 file.
11 * The resulting file should be an exact copy of the input file
12 *
13 */
14#include "HepMC3/GenEvent.h"
15#include "HepMC3/ReaderAscii.h"
16#include "HepMC3/WriterAscii.h"
17#include "HepMC3/Print.h"
18
19#include <iostream>
20using namespace HepMC3;
21
22/** Main program */
23int main(int argc, char **argv) {
24
25 if( argc<3 ) {
26 std::cout << "Usage: " << argv[0] << " <HepMC3_input_file> <output_file>" << std::endl;
27 exit(-1);
28 }
29
30 ReaderAscii input_file (argv[1]);
31 WriterAscii output_file(argv[2]);
32
33 int events_parsed = 0;
34
35 while(!input_file.failed()) {
36 GenEvent evt(Units::GEV,Units::MM);
37
38 // Read event from input file
39 input_file.read_event(evt);
40
41 // If reading failed - exit loop
42 if( input_file.failed() ) break;
43
44 // Save event to output file
45 output_file.write_event(evt);
46
47 if(events_parsed==0) {
48 std::cout << " First event: " << std::endl;
49 Print::listing(evt);
50 Print::content(evt);
51
52 std::cout << " Testing attribute reading for the first event: " << std::endl;
53
54 std::shared_ptr<GenCrossSection> cs = evt.attribute<GenCrossSection>("GenCrossSection");
55 std::shared_ptr<GenHeavyIon> hi = evt.attribute<GenHeavyIon>("GenHeavyIon");
56 std::shared_ptr<GenPdfInfo> pi = evt.attribute<GenPdfInfo>("GenPdfInfo");
57
58 if(cs) {
59 std::cout << " Has GenCrossSection: ";
60 Print::line(cs);
61 }
62 else std::cout << " No GenCrossSection " << std::endl;
63
64 if(pi) {
65 std::cout << " Has GenPdfInfo: ";
66 Print::line(pi);
67 }
68 else std::cout << " No GenPdfInfo " << std::endl;
69
70 if(hi) {
71 std::cout << " Has GenHeavyIon: ";
72 Print::line(hi);
73 }
74 else std::cout << " No GenHeavyIon " << std::endl;
75 }
76
77 ++events_parsed;
78 if( events_parsed%100 == 0 ) std::cout<<"Events parsed: "<<events_parsed<<std::endl;
79 }
80
81 input_file.close();
82 output_file.close();
83
84 return 0;
85}
GenEvent.h
Definition of class GenEvent.
Print.h
Definition of static class Print.
ReaderAscii.h
Definition of class ReaderAscii.
WriterAscii.h
Definition of class WriterAscii.
HepMC3::GenCrossSection
Stores additional information about cross-section.
Definition GenCrossSection.h:42
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::GenHeavyIon
Stores additional information about Heavy Ion generator.
Definition GenHeavyIon.h:26
HepMC3::GenPdfInfo
Stores additional information about PDFs.
Definition GenPdfInfo.h:32
HepMC3::Print::content
static void content(std::ostream &os, const GenEvent &event)
Print content of all GenEvent containers.
Definition Print.cc:17
HepMC3::Print::listing
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition Print.cc:52
HepMC3::Print::line
static void line(std::ostream &os, const GenEvent &event, bool attributes=false)
Print one-line info.
Definition Print.cc:204
HepMC3::ReaderAscii
GenEvent I/O parsing for structured text files.
Definition ReaderAscii.h:31
HepMC3::WriterAscii
GenEvent I/O serialization for structured text files.
Definition WriterAscii.h:25
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24