Belle II Software  release-08-01-10
HepMCReader.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <framework/core/FrameworkExceptions.h>
11 #include <mdst/dataobjects/MCParticleGraph.h>
12 
13 #include <string>
14 #include <fstream>
15 
16 #include "HepMC/GenEvent.h"
17 
18 namespace Belle2 {
32  class HepMCReader {
33 
34  public:
35 
36  //Define exceptions
38  BELLE2_DEFINE_EXCEPTION(HepMCCouldNotOpenFileError, "Could not open file %1% !");
40  BELLE2_DEFINE_EXCEPTION(HepMCInvalidDaughterIndicesError,
41  "Event %1%: Invalid daughter indices d1=%2%, d2=%3%, N=%4% (0<=d1<=d2<=N required)");
43  BELLE2_DEFINE_EXCEPTION(HepMCHeaderNotValidError, "Event %1%: Event header not understood: %2%");
45  BELLE2_DEFINE_EXCEPTION(HepMCConvertFieldError, "Event %1%: Could not convert field %2%: %3%");
47  BELLE2_DEFINE_EXCEPTION(HepMCParticleFormatError, "Event %1%: Particle format not understood, got %2% fields !");
49  BELLE2_DEFINE_EXCEPTION(HepMCInvalidEventError, "Event is invalid.");
51  BELLE2_DEFINE_EXCEPTION(HepMCEmptyEventError,
52  "Event %1%: Number of particles in event is %2% ! (This could mean EOF is reached.) ");
56  HepMCReader(const int minEvent = 0, const int maxEvent = INT_MAX): m_nVirtual(0), m_wrongSignPz(false), m_minEvent(minEvent),
57  m_maxEvent(maxEvent) {}
58 
62  ~HepMCReader() { if (m_input) {m_input.close();} }
63 
68  void open(const std::string& filename);
69 
73  void closeCurrentInputFile() {m_input.close();}
74 
79  int nextValidEvent(HepMC::GenEvent& evt);
86  int getEvent(MCParticleGraph& graph, double& weight);
87 
88  int m_nVirtual;
90  const int m_minEvent;
91  const int m_maxEvent;
93  int countEvents(const std::string& filename);
94  protected:
95 
96  void readNextEvent(HepMC::GenEvent& evt);
97  std::ifstream m_input;
99  };
100 
102 }
103 
Class to read HepMC files and store the content in a MCParticle graph.
Definition: HepMCReader.h:32
void readNextEvent(HepMC::GenEvent &evt)
read the next event from the IO stream and write into evt
Definition: HepMCReader.cc:146
HepMCReader(const int minEvent=0, const int maxEvent=INT_MAX)
Constructor.
Definition: HepMCReader.h:56
int countEvents(const std::string &filename)
Count events in file.
Definition: HepMCReader.cc:162
void open(const std::string &filename)
Opens an ascii file and prepares it for reading.
Definition: HepMCReader.cc:22
void closeCurrentInputFile()
Closes the current input file to allow opening the next one.
Definition: HepMCReader.h:73
std::ifstream m_input
The input stream of the ascii file.
Definition: HepMCReader.h:97
BELLE2_DEFINE_EXCEPTION(HepMCConvertFieldError, "Event %1%: Could not convert field %2%: %3%")
Exception is thrown if a field in the HepMC file could not be converted to a number.
const int m_maxEvent
max events to process
Definition: HepMCReader.h:91
BELLE2_DEFINE_EXCEPTION(HepMCEmptyEventError, "Event %1%: Number of particles in event is %2% ! (This could mean EOF is reached.) ")
Exception is thrown if the number of particles for this event is 0 or less.
~HepMCReader()
Destructor.
Definition: HepMCReader.h:62
BELLE2_DEFINE_EXCEPTION(HepMCHeaderNotValidError, "Event %1%: Event header not understood: %2%")
Exception is thrown if the header specifying the event header could not be parsed.
int getEvent(MCParticleGraph &graph, double &weight)
Reads the next event and stores the result in the given MCParticle graph.
Definition: HepMCReader.cc:44
bool m_wrongSignPz
Bool to indicate that HER and LER were swapped.
Definition: HepMCReader.h:89
int nextValidEvent(HepMC::GenEvent &evt)
Got to next event in bounds.
Definition: HepMCReader.cc:29
BELLE2_DEFINE_EXCEPTION(HepMCInvalidEventError, "Event is invalid.")
Exception is thrown if the format of a line of the HepMC file could not be parsed.
BELLE2_DEFINE_EXCEPTION(HepMCInvalidDaughterIndicesError, "Event %1%: Invalid daughter indices d1=%2%, d2=%3%, N=%4% (0<=d1<=d2<=N required)")
Exception is thrown if the given indices of the daughters are not valid.
BELLE2_DEFINE_EXCEPTION(HepMCCouldNotOpenFileError, "Could not open file %1% !")
Exception is thrown if the HepMC file could not be opened.
const int m_minEvent
min event nr to process
Definition: HepMCReader.h:90
int m_nVirtual
The number of particles in each event with a set Virtual flag.
Definition: HepMCReader.h:88
BELLE2_DEFINE_EXCEPTION(HepMCParticleFormatError, "Event %1%: Particle format not understood, got %2% fields !")
Exception is thrown if the format of a line of the HepMC file could not be parsed.
Class to build, validate and sort a particle decay chain.
Abstract base class for different kinds of events.