HepMC event record
ReaderRoot.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014 The HepMC collaboration (see AUTHORS for details)
5 //
6 /**
7  * @file ReaderRoot.cc
8  * @brief Implementation of \b class ReaderRoot
9  *
10  */
11 #include "HepMC/ReaderRoot.h"
12 
13 namespace HepMC {
14 
15 ReaderRoot::ReaderRoot(const std::string &filename):
16 m_file(filename.c_str()),
17 m_next(m_file.GetListOfKeys()) {
18 
19  if ( !m_file.IsOpen() ) {
20  ERROR( "ReaderRoot: problem opening file: " << filename )
21  return;
22  }
23 
24  shared_ptr<GenRunInfo> ri = make_shared<GenRunInfo>();
25 
26  GenRunInfoData *run = (GenRunInfoData*)m_file.Get("GenRunInfoData");
27 
28  if(run) {
29  ri->read_data(*run);
30  delete run;
31  }
32 
33  set_run_info(ri);
34 }
35 
37 
38  // Skip object of different type than GenEventData
39  GenEventData *data = NULL;
40 
41  while(true) {
42  TKey *key = (TKey*)m_next();
43 
44  if( !key ) {
45  m_file.Close();
46  return false;
47  }
48 
49  const char *cl = key->GetClassName();
50 
51  if( !cl ) continue;
52 
53  if( strncmp(cl,"HepMC::GenEventData",19) == 0 ) {
54  data = (GenEventData*)key->ReadObj();
55  break;
56  }
57  }
58 
59  if( !data ) {
60  ERROR("ReaderRoot: could not read event from root file")
61  m_file.Close();
62  return false;
63  }
64 
65  evt.read_data(*data);
66  evt.set_run_info( run_info() );
67 
68  delete data;
69  return true;
70 }
71 
73  m_file.Close();
74 }
75 
77  if ( !m_file.IsOpen() ) return true;
78 
79  return false;
80 }
81 
82 } // namespace HepMC
void close()
Close file stream.
Definition: ReaderRoot.cc:72
bool failed()
Get stream error state.
Definition: ReaderRoot.cc:76
TFile m_file
File handler.
Definition: ReaderRoot.h:60
Stores serializable run information.
TIter m_next
Iterator for event reading.
Definition: ReaderRoot.h:61
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition of class ReaderRoot.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Stores event-related information.
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition: GenEvent.cc:478
bool read_event(GenEvent &evt)
Read event from file.
Definition: ReaderRoot.cc:36
Definition of template class SmartPointer.
Stores serializable event information.
ReaderRoot(const std::string &filename)
Default constructor.
Definition: ReaderRoot.cc:15
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.