Basic example of use of root I/O: reading events from file.
 
#include "MyClass.h"
#include "MyRunClass.h"
 
#include "TFile.h"
#include "TSystem.h"
#include "TKey.h"
 
#include <iostream>
 
 
 
int main(int argc, char **argv) {
 
    if( argc<3 ) {
        std::cout << "Usage: " << argv[0] << " <input_root_file> <output_hepmc3_file>" << std::endl;
        exit(-1);
    }
 
 
    TFile fo(argv[1]);
 
    int events_parsed = 0;
 
    
    std::shared_ptr<GenRunInfo> run_info;
 
    if( my_run ) run_info.reset(my_run->GetRunInfo());
 
 
    fo.GetListOfKeys()->Print();
 
    TIter next(fo.GetListOfKeys());
    TKey *key = nullptr;
 
    while ((key=(TKey*)next()))
    {
        const char *cl = key->GetClassName();
 
        if( strncmp(cl,"MyClass",7) != 0 ) continue;
 
        fo.GetObject(key->GetName(), myevent);
 
        std::cout << "Event: " << key->GetName() << std::endl;
 
        if( events_parsed == 0 ) {
            std::cout << "First event: " << std::endl;
        }
 
        if( run_info ) {
            std::cout << "Setting run info" << std::endl;
            run_info.reset();
        }
 
        ++events_parsed;
 
        if( events_parsed%100 == 0 ) {
            std::cout << "Event: " << events_parsed << std::endl;
        }
 
        delete myevent;
    }
 
 
    std::cout << "Events parsed and written: " << events_parsed << std::endl;
 
    return 0;
}
Definition of class GenEvent.
Definition of class GenRunInfo.
Definition of static class Print.
Definition of class WriterAscii.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
GenEvent I/O serialization for structured text files.
void close() override
Close file stream.
void write_event(const GenEvent &evt) override
Write event to file.
Sample class for root I/O test.
GenEvent * GetEvent()
Get HepMC event.
Sample class for root I/O test.