HepMC3 event record library: ReaderRoot.cc Source File

HepMC3 event record library

ReaderRoot.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 ReaderRoot.cc
8 * @brief Implementation of \b class ReaderRoot
9 *
10 */
11#include "HepMC3/ReaderRoot.h"
12#include "HepMC3/Version.h"
13
14namespace HepMC3 {
15HEPMC3_DECLARE_READER_FILE(ReaderRoot)
16
17ReaderRoot::ReaderRoot(const std::string &filename) {
18 m_file = TFile::Open(filename.c_str());
19 m_next = new TIter(m_file->GetListOfKeys());
20
21 if ( !m_file->IsOpen() ) {
22 HEPMC3_ERROR_LEVEL(100,"ReaderRoot: problem opening file: " << filename)
23 return;
24 }
25
26 std::shared_ptr<GenRunInfo> ri = std::make_shared<GenRunInfo>();
27
28 auto *run = reinterpret_cast<GenRunInfoData*>(m_file->Get("GenRunInfoData"));
29
30 if (run) {
31 ri->read_data(*run);
32 delete run;
33 }
34
35 set_run_info(ri);
36}
37
38 bool ReaderRoot::skip(const int n)
39{
40 GenEvent evt;
41 for (int nn = n; nn > 0; --nn)
42 {
43 if (!read_event(evt)) return false;
44 evt.clear();
45 }
46 return !failed();
47}
48
49 bool ReaderRoot::read_event(GenEvent& evt) {
50 // Skip object of different type than GenEventData
51 GenEventData *data = nullptr;
52
53 while (true) {
54 TKey *key = (TKey*) (*m_next)();
55
56 if ( !key ) {
57 m_file->Close();
58 return false;
59 }
60
61 const char *cl = key->GetClassName();
62
63 if ( !cl ) continue;
64 size_t geneventdata30 = strncmp(cl, "HepMC::GenEventData", 19);
65 size_t geneventdata31 = strncmp(cl, "HepMC3::GenEventData", 20);
66 if ( geneventdata31 == 0 || geneventdata30 == 0 ) {
67 if (geneventdata30 == 0) {
68 HEPMC3_WARNING_LEVEL(900,"ReaderRoot::read_event: The object was written with HepMC3 version 3.0")
69 }
70 data = reinterpret_cast<GenEventData*>(key->ReadObj());
71 break;
72 }
73 }
74
75 if ( !data ) {
76 HEPMC3_ERROR_LEVEL(100,"ReaderRoot: could not read event from root file")
77 m_file->Close();
78 return false;
79 }
80
81 evt.read_data(*data);
82 evt.set_run_info(run_info());
83
84 delete data;
85 return true;
86}
87
88 void ReaderRoot::close() {
89 m_file->Close();
90}
91
92 bool ReaderRoot::failed() {
93 return !m_file->IsOpen();
94}
95
96} // namespace HepMC3
HEPMC3_WARNING_LEVEL
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition Errors.h:34
HEPMC3_ERROR_LEVEL
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Definition Errors.h:27
ReaderRoot.h
Definition of class ReaderRoot.
Version.h
Declaration of the Verrion functions and some macros.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::GenEvent::set_run_info
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition GenEvent.h:148
HepMC3::GenEvent::read_data
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition protobufUtils.cc:336
HepMC3::GenEvent::clear
void clear()
Remove contents of this event.
Definition GenEvent.cc:592
HepMC3::ReaderRoot
GenEvent I/O parsing and serialization for root files.
Definition ReaderRoot.h:33
HepMC3::ReaderRoot::read_event
bool read_event(GenEvent &evt) override
Read event from file.
Definition ReaderRoot.cc:49
HepMC3::ReaderRoot::failed
bool failed() override
Get stream error state.
Definition ReaderRoot.cc:92
HepMC3::ReaderRoot::skip
bool skip(const int) override
skip events
Definition ReaderRoot.cc:38
HepMC3::ReaderRoot::close
void close() override
Close file stream.
Definition ReaderRoot.cc:88
HepMC3::ReaderRoot::ReaderRoot
ReaderRoot(const std::string &filename)
Default constructor.
Definition ReaderRoot.cc:17
HepMC3::ReaderRoot::m_file
TFile * m_file
File handler.
Definition ReaderRoot.h:63
HepMC3::ReaderRoot::m_next
TIter * m_next
Iterator for event reading.
Definition ReaderRoot.h:64
HepMC3::Reader::set_run_info
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Reader.h:56
HepMC3::Reader::run_info
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Reader.h:44
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::GenEventData
Stores serializable event information.
Definition GenEventData.h:26
HepMC3::GenRunInfoData
Stores serializable run information.
Definition GenRunInfoData.h:23