HepMC3 event record library: AnalysisExample.cc Source File

HepMC3 event record library

AnalysisExample.cc
Go to the documentation of this file.
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/// @file AnalysisExample.cc
8/// @brief Implementation of \b AnalysisExample class with toy physics analysis
9///
10#include "AnalysisExample.h"
11#include <limits>
12namespace HepMC3
13{
14HEPMC3_DECLARE_WRITER_FILE(AnalysisExample)
15HEPMC3_DECLARE_WRITER_STREAM(AnalysisExample)
16
17AnalysisExample::AnalysisExample(const std::string &filename,std::shared_ptr<GenRunInfo> /*run*/): m_file(filename),
18 m_stream(&m_file)
19{
20 if ( !m_file.is_open() ) {
21 HEPMC3_ERROR_LEVEL(100,"AnalysisExample: could not open output file: "<<filename )
22 }
23 m_sum_of_weights=0;
24 m_sum_of_weights2=0;
25 m_bins["rapidity"]=std::vector<double> {-std::numeric_limits<double>::infinity(), -5.0,-4.5,-4.0,-3.5,-3.0,-2.5,-2.0,-1.5,-1.0,-0.5,0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,std::numeric_limits<double>::infinity()};
26 m_vals["rapidity"]=std::vector<double>(m_bins.at("rapidity").size()-1,0.0);
27 m_errs["rapidity"]=std::vector<double>(m_bins.at("rapidity").size()-1,0.0);
28}
29
30 AnalysisExample::AnalysisExample(std::ostream &stream, std::shared_ptr<GenRunInfo> /*run*/)
31 : m_stream(&stream)
32{
33 m_sum_of_weights = 0;
34 m_sum_of_weights2 = 0;
35 m_bins["rapidity"] = std::vector<double> {-std::numeric_limits<double>::infinity(), -5.0,-4.5,-4.0,-3.5,-3.0,-2.5,-2.0,-1.5,-1.0,-0.5,0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,std::numeric_limits<double>::infinity()};
36 m_vals["rapidity"] = std::vector<double>(m_bins.at("rapidity").size()-1,0.0);
37 m_errs["rapidity"] = std::vector<double>(m_bins.at("rapidity").size()-1,0.0);
38
39}
40
41 void AnalysisExample::write_event(const GenEvent &evt)
42{
43 double w = (evt.weights().size() ? evt.weight() : 1.);
44 m_sum_of_weights += w;
45 m_sum_of_weights2 += w*w;
46 for(const auto& p: evt.particles() )
47 {
48 if (p->status() != 1) continue;
49 double eta = p->momentum().eta();
50 int bin = std::distance(m_bins["rapidity"].begin(), lower_bound(m_bins["rapidity"].begin(),m_bins["rapidity"].end(),eta))-1;
51 if (bin < 0) bin = 0;
52 m_vals["rapidity"][bin] += w;
53 m_errs["rapidity"][bin] += w*w;
54 }
55}
56
57 void AnalysisExample::close() {
58 if (!m_stream) return;
59 if (std::abs(m_sum_of_weights) < std::numeric_limits<double>::epsilon()) m_sum_of_weights = 1.0;
60 auto* ofs = dynamic_cast<std::ofstream*>(m_stream);
61 for (size_t i = 1; i < m_vals["rapidity"].size()-1; i++)
62 {
63 double val=m_vals["rapidity"][i]/m_sum_of_weights/(m_bins["rapidity"][i+1]-m_bins["rapidity"][i]);
64 double err=sqrt(m_errs["rapidity"][i])/m_sum_of_weights/(m_bins["rapidity"][i+1]-m_bins["rapidity"][i]);
65 (*ofs)<< std::fixed << std::setprecision( 6 )<<m_bins["rapidity"][i]<<" "<<m_bins["rapidity"][i+1]<<" "<<val<<" "<<err<<std::endl;
66 }
67 if (ofs && !ofs->is_open()) return;
68 if (ofs) ofs->close();
69
70}
71
72} // namespace HepMC3
AnalysisExample.h
Definition of class AnalysisExample, which implements a toy physics analysis.
HepMC3::AnalysisExample
Example analysis. Produces a rapidity distribution of final state particles.
Definition AnalysisExample.h:26
HepMC3::AnalysisExample::m_sum_of_weights2
double m_sum_of_weights2
Sum of event weights**2.
Definition AnalysisExample.h:47
HepMC3::AnalysisExample::AnalysisExample
AnalysisExample(const std::string &filename, std::shared_ptr< GenRunInfo > run)
Constructor.
Definition AnalysisExample.cc:17
HepMC3::AnalysisExample::close
void close() override
Close file stream.
Definition AnalysisExample.cc:57
HepMC3::AnalysisExample::m_file
std::ofstream m_file
Output file.
Definition AnalysisExample.h:52
HepMC3::AnalysisExample::m_errs
std::map< std::string, std::vector< double > > m_errs
Uncertainties.
Definition AnalysisExample.h:50
HepMC3::AnalysisExample::m_vals
std::map< std::string, std::vector< double > > m_vals
Values.
Definition AnalysisExample.h:49
HepMC3::AnalysisExample::m_bins
std::map< std::string, std::vector< double > > m_bins
Binings.
Definition AnalysisExample.h:48
HepMC3::AnalysisExample::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition AnalysisExample.cc:41
HepMC3::AnalysisExample::m_stream
std::ostream * m_stream
Output stream.
Definition AnalysisExample.h:53
HepMC3::AnalysisExample::m_sum_of_weights
double m_sum_of_weights
Sum of event weights.
Definition AnalysisExample.h:46
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::GenEvent::weight
double weight(const unsigned long &index=0) const
Definition GenEvent.h:110
HepMC3::GenEvent::weights
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition GenEvent.h:105
HepMC3::GenEvent::particles
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const).
Definition GenEvent.cc:39
HepMC3::GenRunInfo
Stores run-related information.
Definition GenRunInfo.h:33
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24