HepMC3 event record library
rootIOTree_example_write.cc
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2021 The HepMC collaboration (see AUTHORS for details)
5//
6/**
7 * @file rootIOTree_example_write.cc
8 * @brief Basic example of use of root I/O: writing events to file
9 *
10 * @author Witold Pokorski/Andrii Verbytskyi
11 * @date 29/10/15
12 */
13#include "HepMC3/GenEvent.h"
14#include "HepMC3/ReaderAscii.h"
16#include "HepMC3/Print.h"
17
18#include <iostream>
19
20using namespace HepMC3;
21
22/** Main */
23int main(int argc, char **argv) {
24
25 if( argc<3 ) {
26 std::cout << "Usage: " << argv[0] << " <input_hepmc3_file> <output_root_file>" << std::endl;
27 exit(-1);
28 }
29
30 ReaderAscii text_input (argv[1]);
31 WriterRootTree root_output(argv[2]);
32
33 int events_parsed = 0;
34
35 while( !text_input.failed() ) {
36
37 GenEvent evt(Units::GEV,Units::MM);
38
39 text_input.read_event(evt);
40
41 if( text_input.failed() ) break;
42
43 if( events_parsed == 0 ) {
44 std::cout << "First event: " << std::endl;
45 Print::listing(evt);
46 }
47
48 root_output.write_event(evt);
49 ++events_parsed;
50
51 if( events_parsed%1000 == 0 ) {
52 std::cout << "Event: " << events_parsed << std::endl;
53 }
54 }
55
56 text_input.close();
57 root_output.close();
58
59 std::cout << "Events parsed and written: " << events_parsed << std::endl;
60
61 return 0;
62}
Definition of class GenEvent.
Definition of static class Print.
Definition of class ReaderAscii.
Definition of class WriterRootTree.
Stores event-related information.
Definition: GenEvent.h:41
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:50
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:29
bool read_event(GenEvent &evt) override
Load event from file.
Definition: ReaderAscii.cc:90
bool failed() override
Return status of the stream.
Definition: ReaderAscii.cc:615
void close() override
Close file stream.
Definition: ReaderAscii.cc:617
GenEvent I/O serialization for root files based on root TTree.
void close() override
Close file stream.
void write_event(const GenEvent &evt) override
Write event to file.
HepMC3 main namespace.