/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 | Static Public Member Functions | Private Attributes
RootInputFile Class Reference

Manage one tree in one file. More...

#include <RootInputFile.h>

Collaboration diagram for RootInputFile:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 RootInputFile (const std::string &filename, const std::string &treepath, const std::string &branchname)
 ~RootInputFile ()
const std::string & treepath ()
const std::string & branchname ()
const std::string & filename ()
bool open ()
 Open TFile, get TTree, return false if fail.
bool leave ()
 Stream no longer visit this file.
int entry ()
 Return current entry, -1 if no entry yet set.
bool setEntry (int entry)
 Set entry (but no GetEntry()), return false if illegal value.
bool read (void *addr)
 Read in current entry to the object pointed to by the pointer at the given address addr, return false if entry not properly set.
bool read (void *addr, int &nbytes)
 Read as above, fill nbytes with number of bytes written.
bool setAddr (void *addr)
 Set address of pointer to object to copy branch name into on read().
int entries ()
 Return total number of entries.
bool next (int steps=1)
 Advance entry number by given number of steps, return false if too far.
bool beginning ()
 Go to first entry.
bool ending ()
 Go to last entry.
bool prev (int steps=1)
 Retard entry number by given number of steps, return false if too far.

Static Public Member Functions

static std::vector< std::string > TreePaths (const std::string &filename)
static int TestForObject (const std::string &filename, const std::string &treepath, const std::string &branchname, bool ignore_missing=false)

Private Attributes

std::string m_filename
std::string m_treepath
std::string m_branchname
int m_pruneSimHits
TFile * m_file
TTree * m_tree
int m_entry
int m_entries
void * m_addr
Dyb::MsgStreamMember log

Detailed Description

Manage one tree in one file.

See RootInputStream for more useful class.

bv@bnl.gov Sun Jun 29 10:12:20 2008

Definition at line 26 of file RootInputFile.h.


Constructor & Destructor Documentation

RootInputFile::RootInputFile ( const std::string &  filename,
const std::string &  treepath,
const std::string &  branchname 
)

Definition at line 267 of file RootInputFile.cc.

    : m_filename(filename)
    , m_treepath(treepath)
    , m_branchname(branchname)
    , m_pruneSimHits(0)
    , m_file(0), m_tree(0)
    , m_entry(-1), m_entries(-1)
    , m_addr(0)
    , log("RootInputFile")
{
  //Prune SimHitHeader when reading
  char* pruneSimHitHeader = getenv("NUWA_PRUNESIMHITHEADER");
  if(0 != pruneSimHitHeader) m_pruneSimHits = 1;
}
RootInputFile::~RootInputFile ( )

Definition at line 283 of file RootInputFile.cc.

{
}

Member Function Documentation

std::vector< std::string > RootInputFile::TreePaths ( const std::string &  filename) [static]

Definition at line 148 of file RootInputFile.cc.

{
    std::vector<std::string> ret;


    TFile* f = TFile::Open(filename.c_str(),"READ");
    if (!f->IsOpen()) {
        std::cerr << "File " << filename << " can not be opened for reading\n";
        return ret;
    }
    //std::cerr << "File " << filename << " openeded\n";
    
    process_directory(f,ret);
    return ret;
}
int RootInputFile::TestForObject ( const std::string &  filename,
const std::string &  treepath,
const std::string &  branchname,
bool  ignore_missing = false 
) [static]

Definition at line 192 of file RootInputFile.cc.

{
    Dyb::MsgStreamMember log("RootInputFile::TestForTObject");


    TFile *f = TFile::Open(filename.c_str(),"READ");
    if (!f->IsOpen()) {
        log << MSG::ERROR 
            << "File " << filename 
            << " can not be opened for reading" 
            << endreq;
        return 0;
    }
    log << MSG::DEBUG  << "File " << filename
        << " opened. will now get obj at treepath is " << treepath 
        << endreq;

    TObject* obj = 0;
    if (treepath.rfind('/') == 0) {
        // Catch root-level trees
        log << MSG::VERBOSE << "Try to get root-level tree. get obj at treepath " 
            << treepath.substr(1).c_str() << endreq;
        obj = f->Get(treepath.substr(1).c_str());
    }
    else {
        log << MSG::VERBOSE << "Get obj at treepath " << treepath.c_str() << endreq;
        obj = f->Get(treepath.c_str());
    }
    if (!obj) {
        if (!ignore_missing) 
            log << MSG::ERROR
                << "No object " << treepath
                << " in file " << filename << endreq;
        return 0;
    }
    log << MSG::DEBUG << "Got object at " << treepath << endreq;
    
    TTree* tree = dynamic_cast<TTree*>(obj);
    if (!tree) {
        log << MSG::ERROR
            << "Object at " << treepath
            << " in file " << filename 
            << " is not a TTree" << endreq;
        return 0;
    }
    //cerr << "Object is a TTree\n";

    int nentries = tree->GetEntries();
    if (!nentries) {
        log << MSG::ERROR
            << "Tree " << treepath 
            << " in filename " << filename 
            << " found, but no entries" << endreq;
        return 0;
    }
    //cerr << "Tree has " << nentries << " entries\n";

    TBranch* b = tree->GetBranch(branchname.c_str());
    if (!b) {
        log << MSG::ERROR
            << "Failed to get branch " << branchname << endreq;
        return 0;
    }
    //cerr << "search for uniq id" << std::endl;

    int ret = find_uniq(b);
    f->Close();
    delete f;
    return ret;
}
const std::string& RootInputFile::treepath ( ) [inline]

Definition at line 57 of file RootInputFile.h.

{ return m_treepath; }
const std::string& RootInputFile::branchname ( ) [inline]

Definition at line 58 of file RootInputFile.h.

{ return m_branchname; }
const std::string& RootInputFile::filename ( ) [inline]

Definition at line 59 of file RootInputFile.h.

{ return m_filename; }
bool RootInputFile::open ( )

Open TFile, get TTree, return false if fail.

Definition at line 287 of file RootInputFile.cc.

{
    // is this a dummy entry in the input stream?
    if (m_filename == "") {
        m_entries = 0;
        return true;
    }

    if (!m_file) {
        log << MSG::DEBUG
            << "openning " << m_filename << " for " << m_treepath 
            << endreq;

        m_file = TFile::Open(m_filename.c_str(),"READ");
    }
    if (!m_tree) {
        std::string treepath = m_treepath;
        // Catch top-level objects
        if(treepath.rfind("/") == 0) treepath = treepath.substr(1);
        TObject* obj = m_file->Get(treepath.c_str());
        if (!obj) {
            log << MSG::ERROR << "open(): No such object at " << m_treepath
                << " in " << m_filename << endreq;
            return false;
        }
        m_tree = dynamic_cast<TTree*>(obj);
        if (!m_tree) {
            log << MSG::ERROR
                << "open(): Object at " << m_treepath
                << " in " << m_filename 
                << " not a TTree\n";
            return false;
        }
        if (m_entries < 0) {
            m_entries = m_tree->GetEntries();
        }
    }
    if (!m_tree) return false;

    handle_user_data(m_tree,m_treepath);

    return true;
}
bool RootInputFile::leave ( )

Stream no longer visit this file.

Definition at line 331 of file RootInputFile.cc.

{
    log << MSG::DEBUG
        << "leaving " << m_filename << " for " << m_treepath
        << endreq;

    // The opened file must be closed to avoid memory leak
    if(m_file) {
        m_file->Close();
        delete m_file;
        m_file = 0;
    }
    m_tree = 0;
    m_addr = 0;
    m_entry = -1;

    return true;
}
int RootInputFile::entry ( )

Return current entry, -1 if no entry yet set.

Definition at line 350 of file RootInputFile.cc.

{
    return m_entry;
}
bool RootInputFile::setEntry ( int  entry)

Set entry (but no GetEntry()), return false if illegal value.

Definition at line 355 of file RootInputFile.cc.

{
    if (!this->open()) return false;
    if (entry >= m_entries) {
        log << MSG::ERROR << "setEntry("<<entry<<"): Entry too large >= " << m_entries
            << endreq;
        return false;
    }
    m_entry = entry;
    return true;
}
bool RootInputFile::read ( void *  addr)

Read in current entry to the object pointed to by the pointer at the given address addr, return false if entry not properly set.

Definition at line 397 of file RootInputFile.cc.

{
    int nbytes = 0;
    return this->read(addr,nbytes);
}
bool RootInputFile::read ( void *  addr,
int &  nbytes 
)

Read as above, fill nbytes with number of bytes written.

Definition at line 403 of file RootInputFile.cc.

{
    nbytes = 0;
    if (!this->open()) return false;
    if (m_entry < 0 || m_entry >= m_entries) {
        log << MSG::ERROR << "read(void*): Bad entry: " << m_entry << endreq;
        return false;
    }

    if (!this->setAddr(addr)) return false;

    log << MSG::DEBUG
        << "read("<<(void*)addr<<") entry=" << m_entry
        << " from file " << m_filename
        << " and branch " << m_branchname
        << endreq;
    nbytes = m_tree->GetEntry(m_entry);
    return nbytes > 0;
}
bool RootInputFile::setAddr ( void *  addr)

Set address of pointer to object to copy branch name into on read().

Definition at line 368 of file RootInputFile.cc.

{
    if (addr == m_addr) return true;
    TBranch* b = m_tree->GetBranch(m_branchname.c_str());
    if (!b) {
        log << MSG::ERROR << "setAddr(): no such branch named " 
            << m_branchname << endreq;
        return false;
    }
    
    log << MSG::DEBUG 
        << "setAddr("<<(void*)addr<<") for " << m_branchname << " in " << m_filename
        << endreq;


    if(1 == m_pruneSimHits && m_branchname == "Sim_SimHeader")
    {
      TBranch* hits = b->FindBranch("hits");
      if(hits)
      {
        hits->DeleteBaskets("all");
        m_pruneSimHits = 2;
      }
    }           
    b->SetAddress(addr);
    m_addr = addr;
    return true;
}
int RootInputFile::entries ( )

Return total number of entries.

Definition at line 423 of file RootInputFile.cc.

{
    if (m_entries < 0) {        // never opened this file
        if (!this->open()) return 0;
        this->leave();          // may not want to leave open long term, leave it as found it
    }
    return m_entries;
}
bool RootInputFile::next ( int  steps = 1)

Advance entry number by given number of steps, return false if too far.

Definition at line 432 of file RootInputFile.cc.

{
    return this->setEntry(m_entry+steps);
}
bool RootInputFile::beginning ( )

Go to first entry.

Definition at line 437 of file RootInputFile.cc.

{
    if (!this->open()) return false;
    m_entry = 0;
    return true;
}
bool RootInputFile::ending ( )

Go to last entry.

Definition at line 443 of file RootInputFile.cc.

{
    if (!this->open()) return false;
    m_entry = m_entries-1;
    return true;
}
bool RootInputFile::prev ( int  steps = 1)

Retard entry number by given number of steps, return false if too far.

Definition at line 451 of file RootInputFile.cc.

{
    return this->setEntry(m_entry-steps);
}

Member Data Documentation

std::string RootInputFile::m_filename [private]

Definition at line 27 of file RootInputFile.h.

std::string RootInputFile::m_treepath [private]

Definition at line 28 of file RootInputFile.h.

std::string RootInputFile::m_branchname [private]

Definition at line 29 of file RootInputFile.h.

Definition at line 30 of file RootInputFile.h.

TFile* RootInputFile::m_file [private]

Definition at line 31 of file RootInputFile.h.

TTree* RootInputFile::m_tree [private]

Definition at line 32 of file RootInputFile.h.

int RootInputFile::m_entry [private]

Definition at line 33 of file RootInputFile.h.

int RootInputFile::m_entries [private]

Definition at line 33 of file RootInputFile.h.

void* RootInputFile::m_addr [private]

Definition at line 34 of file RootInputFile.h.

Definition at line 36 of file RootInputFile.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