/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes
RootOutputStream Class Reference

A generalized stream of output data. More...

#include <RootOutputStream.h>

Inheritance diagram for RootOutputStream:
Inheritance graph
[legend]
Collaboration diagram for RootOutputStream:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 RootOutputStream (void *addr, const std::string &classname, const std::string &treepath="", const std::string &branchname="")
 Create a root output stream with the address of a pointer to an object.
virtual ~RootOutputStream ()
void setIndexExpression (const std::string &major, const std::string &minor)
 Set the major/minor index expressions for passing to TTree::BuildIndex.
std::string path ()
 RootIOStream interface.
bool write ()
 Write current state of addr to disk.
bool close ()
 Write tree to file and close file.
bool newFile (const std::string &filename, const std::string &treepath="", const std::string &branchname="")
 Start a new output file.
int entries ()
 Return the number of entries in the output stream.
int fileEntries ()
 Return the number of entries in the current output file.
RootIOBaseObjectobj ()
 Return the pointer that m_addr points to assuming it points to a RootIOBaseObject.

Protected Attributes

void * m_addr

Private Member Functions

bool handle_user_data ()

Private Attributes

TFile * m_file
TTree * m_tree
TDirectory * m_dir
std::string m_classname
std::string m_treepath
std::string m_branchname
int m_entries
Dyb::MsgStreamMember log
std::string m_majorIndex
std::string m_minorIndex

Detailed Description

A generalized stream of output data.

This allows writing data out to file. The output stream can be broken up in to multiple files.

bv@bnl.gov Sun Jun 29 10:13:17 2008

Definition at line 26 of file RootOutputStream.h.


Constructor & Destructor Documentation

RootOutputStream::RootOutputStream ( void *  addr,
const std::string &  classname,
const std::string &  treepath = "",
const std::string &  branchname = "" 
)

Create a root output stream with the address of a pointer to an object.

Optionally specify the treepath and branchname

Definition at line 16 of file RootOutputStream.cc.

    : RootIOStream(addr)
    , m_file(0)
    , m_tree(0)
    , m_dir(0)
    , m_classname(classname)
    , m_treepath(treepath)
    , m_branchname(branchname)
    , m_entries(0)
    , log("RootOutputStream")
    , m_majorIndex("")
    , m_minorIndex("")
{
    TClass* cl = TClass::GetClass(classname.c_str());
    if (!cl) {
        log << MSG::ERROR << "Failed to get class for " 
            << classname << endreq;
    }

}
RootOutputStream::~RootOutputStream ( ) [virtual]

Definition at line 41 of file RootOutputStream.cc.

{
    this->close();
}

Member Function Documentation

void RootOutputStream::setIndexExpression ( const std::string &  major,
const std::string &  minor 
)

Set the major/minor index expressions for passing to TTree::BuildIndex.

Definition at line 46 of file RootOutputStream.cc.

{
    m_majorIndex = majorIndex;
    m_minorIndex = minorIndex;
}
string RootOutputStream::path ( ) [virtual]

RootIOStream interface.

Implements RootIOStream.

Definition at line 52 of file RootOutputStream.cc.

{
    return m_treepath;
}
bool RootOutputStream::write ( )

Write current state of addr to disk.

Definition at line 59 of file RootOutputStream.cc.

{
    if (!m_tree) {
        log << MSG::ERROR << "write(): no tree" 
            << endreq;
        return false;
    }

    int nbytes = m_tree->Fill();
    log << MSG::DEBUG << "Wrote " << nbytes
        << " bytes to entry " << m_entries
        << " of tree " << m_treepath
        << endreq;

    // Debugging #445.
    //TObject* dir = m_file->Get("Event/Sim");
    //if (dir) dir->ls();

    ++m_entries;
    return nbytes > 0;    
}
bool RootOutputStream::close ( )

Write tree to file and close file.

Definition at line 91 of file RootOutputStream.cc.

{
    if (!m_file) return true;
    if (!m_tree) return true;
    
    log << MSG::DEBUG << "Closing file " << m_file->GetName() << endreq;

    if (m_majorIndex.size() || m_minorIndex.size()) {
        log << MSG::DEBUG
            << "Building index using major=\"" << m_majorIndex
            << "\", minor=\"" << m_minorIndex << "\"" << endreq;
        m_tree->BuildIndex(m_majorIndex.c_str(),m_minorIndex.c_str());
    }

    m_dir->cd();
    m_tree->Write(NULL,TObject::kOverwrite); // for kOverwrite see #445
    RootOutputFileManager::get().close_file(m_file);
    m_file = 0;
    m_tree = 0;
    m_dir = 0;
    return true;
}
bool RootOutputStream::newFile ( const std::string &  filename,
const std::string &  treepath = "",
const std::string &  branchname = "" 
)

Start a new output file.

Definition at line 116 of file RootOutputStream.cc.

{
    // Only start a new file if filename differes
    if (m_file && filename == m_file->GetName()) {
        return true;
    }

    if ("" != treepath) m_treepath = treepath;
    if ("" != branchname) m_branchname = branchname;

    if ("" == m_treepath || "" == m_branchname) {
        log << MSG::ERROR 
            << "newFile(): tree path or branch name are empty"
            << endreq;
        return false;
    }

    this->close();

    m_file = RootOutputFileManager::get().get_file(filename);

    // Make the directories up to but not including last one which is
    // the tree name.
    m_dir = m_file;
    string::size_type last = 0, slash = m_treepath.find('/');
    for (; slash != string::npos; slash = m_treepath.find('/',last)) {
        if (!slash) {
            last = slash + 1;
            continue;   // skip initial '/'
        }
        string subdir = m_treepath.substr(last,slash-last);
        TDirectory* dir = m_dir->GetDirectory(subdir.c_str());
        if (dir) m_dir = dir;
        else m_dir = m_dir->mkdir(subdir.c_str());
        last = slash + 1;
    }
    if (last) {
        string subdir = m_treepath.substr(0,last-1);
        m_file->cd(subdir.c_str());
    }
        
    string title = "Tree at " + m_treepath + " holding " + m_branchname;
    string treename = m_treepath.substr(last);

    m_tree = new TTree(treename.c_str(),title.c_str());
    m_tree->Branch(m_branchname.c_str(),m_classname.c_str(),m_addr);

    bool ok = handle_user_data();
    if (!ok) return false;

    log << MSG::DEBUG
        << "newFile(): " << title << " in file " << filename
        << endreq;

    return true;
}
int RootOutputStream::entries ( )

Return the number of entries in the output stream.

Definition at line 81 of file RootOutputStream.cc.

{
    return m_entries;
}
int RootOutputStream::fileEntries ( )

Return the number of entries in the current output file.

Definition at line 86 of file RootOutputStream.cc.

{
    return m_tree->GetEntries();
}
bool RootOutputStream::handle_user_data ( ) [private]

Definition at line 175 of file RootOutputStream.cc.

{
    RootIOUserData ud;
    RootIOUserData::ProxyCollection& proxies = ud.output(m_treepath);
    RootIOUserData::ProxyCollection::iterator it, done = proxies.end();

    log << MSG::DEBUG << "newFile(): handling user data with " 
        << proxies.size() << " proxies at treepath=" << m_treepath
        << " (one of " << ud.outputMap().size() << ")"
        << endreq;

    bool ret = true;
    
    for (it = proxies.begin(); it != done; ++it) {
        RootIOUserDataProxy* udp = it->second;
        bool ok = udp->branch(m_tree);
        if (ok) {
            log << MSG::DEBUG << "newFile(): initiated user data type: "
                << udp->cppType() << " name: " << udp->varName() << endreq;
        }
        else {
            log << MSG::ERROR << "newFile(): failed to initiate user data type: "
                << udp->cppType() << " name: " << udp->varName() << endreq;
            ret = false;
        }
    }
    return ret;
}
RootIOBaseObject * RootIOStream::obj ( ) [inherited]

Return the pointer that m_addr points to assuming it points to a RootIOBaseObject.

Definition at line 13 of file RootIOStream.cc.

{
    return *(RootIOBaseObject**)m_addr;
}

Member Data Documentation

TFile* RootOutputStream::m_file [private]

Definition at line 28 of file RootOutputStream.h.

TTree* RootOutputStream::m_tree [private]

Definition at line 29 of file RootOutputStream.h.

TDirectory* RootOutputStream::m_dir [private]

Definition at line 30 of file RootOutputStream.h.

std::string RootOutputStream::m_classname [private]

Definition at line 32 of file RootOutputStream.h.

std::string RootOutputStream::m_treepath [private]

Definition at line 32 of file RootOutputStream.h.

std::string RootOutputStream::m_branchname [private]

Definition at line 32 of file RootOutputStream.h.

Definition at line 34 of file RootOutputStream.h.

Definition at line 36 of file RootOutputStream.h.

std::string RootOutputStream::m_majorIndex [private]

Definition at line 71 of file RootOutputStream.h.

std::string RootOutputStream::m_minorIndex [private]

Definition at line 71 of file RootOutputStream.h.

void* RootIOStream::m_addr [protected, inherited]

Definition at line 10 of file RootIOStream.h.


The documentation for this class was generated from the following files:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:58:18 for RootIOSvc by doxygen 1.7.4