MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
cmd_print.cc
1
4//
5// This file is part of MARLEY (Model of Argon Reaction Low Energy Yields)
6//
7// MARLEY is free software: you can redistribute it and/or modify it under the
8// terms of version 3 of the GNU General Public License as published by the
9// Free Software Foundation.
10//
11// For the full text of the license please see COPYING or
12// visit http://opensource.org/licenses/GPL-3.0
13//
14// Please respect the MCnet academic usage guidelines. See GUIDELINES
15// or visit https://www.montecarlonet.org/GUIDELINES for details.
16
17// Standard library includes
18#include <iostream>
19#include <string>
20#include <vector>
21
22// HepMC3 includes
23#include "HepMC3/Attribute.h"
24#include "HepMC3/GenEvent.h"
25#include "HepMC3/GenParticle.h"
26#include "HepMC3/PrintStreams.h"
27
28// MARLEY includes
29#include "marley/CommandHandler.hh"
30#include "marley/EventFileReader.hh"
31#include "marley/hepmc3_utils.hh"
32
33namespace {
34
35 enum class PrintFormat {
36 Pretty,
37 HepMC3,
38 Legacy
39 };
40
41 void print_particle_info( HepMC3::GenParticle& p ) {
42 const auto& p4 = p.momentum();
43 std::cout << " particle with PDG code = " << p.pid()
44 << " has total energy " << p4.e() << " MeV,"
45 << '\n' << " 3-momentum = (" << p4.px() << " MeV, " << p4.py()
46 << " MeV, " << p4.pz() << " MeV)," << '\n'
47 << " mass = " << p.generated_mass() << " MeV, and charge = "
48 << marley_hepmc3::get_particle_charge( p )
49 << " times the proton charge." << '\n';
50 }
51
52 void print_event_info( HepMC3::GenEvent& e, const size_t num ) {
53
54 const auto initials_proj = marley_hepmc3::get_particles_with_status(
55 marley_hepmc3::NUHEPMC_PROJECTILE_STATUS, e );
56 const auto initials_targ = marley_hepmc3::get_particles_with_status(
57 marley_hepmc3::NUHEPMC_TARGET_STATUS, e );
58 const auto finals = marley_hepmc3::get_particles_with_status(
59 marley_hepmc3::NUHEPMC_FINAL_STATE_STATUS, e );
60 auto residue = marley_hepmc3::get_residue( e );
61
62 size_t num_initial = initials_proj.size() + initials_targ.size();
63 size_t num_final = finals.size();
64
65 std::cout << "\n*** Event " << num << " has "
66 << num_initial << " initial particles and "
67 << num_final << " final particles. ***" << '\n';
68
69 int twoJ = 0;
70 double Ex = 0.;
71 int parity = 0;
72 if ( residue ) {
73 auto Ex_attr = residue->attribute< HepMC3::DoubleAttribute >( "Ex" );
74 if ( Ex_attr ) Ex = Ex_attr->value();
75 auto twoJ_attr = residue->attribute< HepMC3::IntAttribute >( "twoJ" );
76 if ( twoJ_attr ) twoJ = twoJ_attr->value();
77 auto parity_attr = residue->attribute< HepMC3::IntAttribute >( "parity" );
78 if ( parity_attr ) parity = parity_attr->value();
79
80 bool twoJ_is_odd = ( twoJ % 2 == 1 );
81
82 std::cout << "The residual nucleus initially had excitation energy "
83 << Ex << " MeV and spin-parity ";
84 if ( twoJ_is_odd ) std::cout << twoJ << "/2";
85 else std::cout << twoJ / 2;
86 std::cout << ( parity >= 0 ? '+' : '-' ) << '\n';
87 }
88
89 std::cout << "Initial particles" << '\n';
90 for ( const auto& particle_i : initials_proj ) {
91 print_particle_info( *particle_i );
92 }
93 for ( const auto& particle_i : initials_targ ) {
94 print_particle_info( *particle_i );
95 }
96
97 std::cout << "Final particles" << '\n';
98 for ( const auto& particle_f : finals ) {
99 print_particle_info( *particle_f );
100 }
101 }
102
103}
104
105bool marley::CommandHandler::cmd_print( std::deque< std::string >& args ) {
106
107 std::string first_arg;
108 if ( !args.empty() ) first_arg = args.front();
109
110 if ( first_arg.empty() || first_arg == "-h" || first_arg == "--help" ) {
111 args.clear();
112 args.push_front( "print" );
114 }
115
116 PrintFormat format = PrintFormat::Pretty;
117 if ( first_arg == "pretty" ) {
118 // Pretty is the default format (set above), so just
119 // drop the format specifier from the input arguments
120 args.pop_front();
121 }
122 else if ( first_arg == "hepmc3" ) {
123 format = PrintFormat::HepMC3;
124 args.pop_front();
125 }
126 else if ( first_arg == "legacy" ) {
127 format = PrintFormat::Legacy;
128 args.pop_front();
129 }
130
131 // If there are no input files listed, print the help message
132 // and signal that an error condition was encountered
133 if ( args.empty() ) {
134 args.push_front( "print" );
136 return false;
137 }
138
139 // Now do the actual printing by iterating over the events in each
140 // input file specified in the remaining command-line arguments
141 for ( const auto& file_name : args ) {
142 marley::EventFileReader reader( file_name );
144 int event_number = 0;
145 while ( reader >> ev ) {
146 if ( format == PrintFormat::Pretty ) {
147 marley_hepmc3::print_event( ev );
148 }
149 else if ( format == PrintFormat::HepMC3 ) {
150 std::cout << ev;
151 }
152 else {
153 // Legacy format
154 print_event_info( ev, event_number );
155 ++event_number;
156 }
157 }
158 }
159 return true;
160}
Attribute that holds a real number as a double.
Definition Attribute.h:245
double value() const
get the value associated to this Attribute.
Definition Attribute.h:271
Stores event-related information.
Definition GenEvent.h:47
Stores particle-related information.
Definition GenParticle.h:34
const FourVector & momentum() const
Get momentum.
Definition GenParticle.h:97
double generated_mass() const
Get generated mass.
int pid() const
Get PDG ID.
Definition GenParticle.h:94
Attribute that holds an Integer implemented as an int.
Definition Attribute.h:157
int value() const
get the value associated to this Attribute.
Definition Attribute.h:180
static bool cmd_help(std::deque< std::string > &args)
Display top-level or command-specific help messages.
Definition cmd_help.cc:24
static bool cmd_print(std::deque< std::string > &args)
Print existing MARLEY events in a human-readable format.
Definition cmd_print.cc:105
Object that parses MARLEY output files.