File: WriterHEPEVT.cc

package info (click to toggle)
hepmc3 3.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 14,116 kB
  • sloc: fortran: 66,849; cpp: 13,604; ansic: 1,374; xml: 109; sh: 72; makefile: 33
file content (87 lines) | stat: -rw-r--r-- 3,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// -*- C++ -*-
//
// This file is part of HepMC
// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
//
/**
 *  @file WriterHEPEVT.cc
 *  @brief Implementation of \b class WriterHEPEVT
 *
 */
#include <sstream>
#include <cstdio>  // sprintf
#include "HepMC3/WriterHEPEVT.h"
#include "HepMC3/HEPEVT_Wrapper.h"
#include "HepMC3/Print.h"
namespace HepMC3
{


WriterHEPEVT::WriterHEPEVT(const std::string &filename): m_events_count(0),m_vertices_positions_present(true), m_file(filename), m_stream(&m_file)
{
    hepevtbuffer=(char*)(new struct HEPEVT());
    HEPEVT_Wrapper::set_hepevt_address(hepevtbuffer);
}

WriterHEPEVT::WriterHEPEVT(std::ostream& stream): m_events_count(0),m_vertices_positions_present(true), m_file(), m_stream(&stream)
{
    hepevtbuffer=(char*)(new struct HEPEVT());
    HEPEVT_Wrapper::set_hepevt_address(hepevtbuffer);
}

void WriterHEPEVT::write_hepevt_particle( int index, bool iflong )
{
    char buf[512];//Note: the format is fixed, so no reason for complicatied tratment
    char* cursor=&(buf[0]);
    cursor +=sprintf(cursor, "% 8i% 8i",HEPEVT_Wrapper::status(index), HEPEVT_Wrapper::id(index));
    if (iflong)
    {
        cursor +=sprintf(cursor,"% 8i% 8i",HEPEVT_Wrapper::first_parent(index),HEPEVT_Wrapper::last_parent(index));
        cursor +=sprintf(cursor,"% 8i% 8i",HEPEVT_Wrapper::first_child(index),HEPEVT_Wrapper::last_child(index));
        cursor +=sprintf(cursor,"% 19.8E% 19.8E% 19.8E% 19.8E% 19.8E\n",HEPEVT_Wrapper::px(index),HEPEVT_Wrapper::py(index),HEPEVT_Wrapper::pz(index),HEPEVT_Wrapper::e(index),HEPEVT_Wrapper::m(index));
        cursor +=sprintf(cursor, "%-48s% 19.8E% 19.8E% 19.8E% 19.8E\n"," ",HEPEVT_Wrapper::x(index),HEPEVT_Wrapper::y(index),HEPEVT_Wrapper::z(index),HEPEVT_Wrapper::t(index));
    }
    else
    {
        cursor +=sprintf(cursor, "% 8i% 8i",HEPEVT_Wrapper::first_child(index),HEPEVT_Wrapper::last_child(index));
        cursor +=sprintf(cursor, "% 19.8E% 19.8E% 19.8E% 19.8E\n",HEPEVT_Wrapper::px(index),HEPEVT_Wrapper::py(index),HEPEVT_Wrapper::pz(index),HEPEVT_Wrapper::m(index));
    }
    unsigned long length = cursor - &(buf[0]);
    m_stream->write( buf, length );
}

void WriterHEPEVT::write_hepevt_event_header()
{
    char buf[512];//Note: the format is fixed, so no reason for complicatied tratment
    char* cursor=buf;
    cursor +=sprintf(cursor,"E% 8i %8i\n",HEPEVT_Wrapper::event_number(),HEPEVT_Wrapper::number_entries());
    unsigned long length = cursor - &(buf[0]);
    m_stream->write( buf, length );
}

void WriterHEPEVT::write_event(const GenEvent &evt)
{
    HEPEVT_Wrapper::GenEvent_to_HEPEVT(&evt);
    HEPEVT_Wrapper::fix_daughters();
    write_hepevt_event_header();
    for( int i=1; i<=HEPEVT_Wrapper::number_entries(); ++i )  write_hepevt_particle(i,m_vertices_positions_present);
    m_events_count++;
}

void WriterHEPEVT::close()
{
    std::ofstream* ofs = dynamic_cast<std::ofstream*>(m_stream);
    if (ofs && !ofs->is_open()) return;
    if (ofs) ofs->close();
}

bool WriterHEPEVT::failed()
{
    return (bool)m_file.rdstate();
}

void WriterHEPEVT::set_vertices_positions_present(bool iflong) {m_vertices_positions_present=iflong;}

bool WriterHEPEVT::get_vertices_positions_present() const { return m_vertices_positions_present;}

} // namespace HepMC3