#include <RootInputFile.h>
Collaboration diagram for RootInputFile:
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) |
Private Attributes | |
std::string | m_filename |
std::string | m_treepath |
std::string | m_branchname |
TFile * | m_file |
TTree * | m_tree |
int | m_entry |
int | m_entries |
void * | m_addr |
Dyb::MsgStreamMember | log |
See RootInputStream for more useful class.
bv@bnl.gov Sun Jun 29 10:12:20 2008
Definition at line 26 of file RootInputFile.h.
RootInputFile::RootInputFile | ( | const std::string & | filename, | |
const std::string & | treepath, | |||
const std::string & | branchname | |||
) |
Definition at line 246 of file RootInputFile.cc.
00249 : m_filename(filename) 00250 , m_treepath(treepath) 00251 , m_branchname(branchname) 00252 , m_file(0), m_tree(0) 00253 , m_entry(-1), m_entries(-1) 00254 , m_addr(0) 00255 , log("RootInputFile") 00256 { 00257 }
RootInputFile::~RootInputFile | ( | ) |
std::vector< std::string > RootInputFile::TreePaths | ( | const std::string & | filename | ) | [static] |
Definition at line 129 of file RootInputFile.cc.
00130 { 00131 std::vector<std::string> ret; 00132 00133 00134 TFile* f = TFile::Open(filename.c_str(),"READ"); 00135 if (!f->IsOpen()) { 00136 std::cerr << "File " << filename << " can not be opened for reading\n"; 00137 return ret; 00138 } 00139 //std::cerr << "File " << filename << " openeded\n"; 00140 00141 process_directory(f,ret); 00142 return ret; 00143 }
int RootInputFile::TestForObject | ( | const std::string & | filename, | |
const std::string & | treepath, | |||
const std::string & | branchname | |||
) | [static] |
Definition at line 173 of file RootInputFile.cc.
00176 { 00177 Dyb::MsgStreamMember log("RootInputFile::TestForTObject"); 00178 00179 00180 TFile *f = TFile::Open(filename.c_str(),"READ"); 00181 if (!f->IsOpen()) { 00182 log << MSG::ERROR 00183 << "File " << filename 00184 << " can not be opened for reading" 00185 << endreq; 00186 return 0; 00187 } 00188 log << MSG::DEBUG << "File " << filename 00189 << " opened. will now get obj at treepath is " << treepath 00190 << endreq; 00191 00192 TObject* obj = 0; 00193 if (treepath.rfind('/') == 0) { 00194 // Catch root-level trees 00195 log << MSG::VERBOSE << "Try to get root-level tree. get obj at treepath " 00196 << treepath.substr(1).c_str() << endreq; 00197 obj = f->Get(treepath.substr(1).c_str()); 00198 } 00199 else { 00200 log << MSG::VERBOSE << "Get obj at treepath " << treepath.c_str() << endreq; 00201 obj = f->Get(treepath.c_str()); 00202 } 00203 if (!obj) { 00204 log << MSG::ERROR 00205 << "No object " << treepath 00206 << " in file " << filename << endreq; 00207 return 0; 00208 } 00209 log << MSG::DEBUG << "Got object at " << treepath << endreq; 00210 00211 TTree* tree = dynamic_cast<TTree*>(obj); 00212 if (!tree) { 00213 log << MSG::ERROR 00214 << "Object at " << treepath 00215 << " in file " << filename 00216 << " is not a TTree" << endreq; 00217 return 0; 00218 } 00219 //cerr << "Object is a TTree\n"; 00220 00221 int nentries = tree->GetEntries(); 00222 if (!nentries) { 00223 log << MSG::ERROR 00224 << "Tree " << treepath 00225 << " in filename " << filename 00226 << " found, but no entries" << endreq; 00227 return 0; 00228 } 00229 //cerr << "Tree has " << nentries << " entries\n"; 00230 00231 TBranch* b = tree->GetBranch(branchname.c_str()); 00232 if (!b) { 00233 log << MSG::ERROR 00234 << "Failed to get branch " << branchname << endreq; 00235 return 0; 00236 } 00237 //cerr << "search for uniq id" << std::endl; 00238 00239 int ret = find_uniq(b); 00240 f->Close(); 00241 delete f; 00242 return ret; 00243 }
const std::string& RootInputFile::treepath | ( | ) | [inline] |
const std::string& RootInputFile::branchname | ( | ) | [inline] |
const std::string& RootInputFile::filename | ( | ) | [inline] |
bool RootInputFile::open | ( | ) |
Open TFile, get TTree, return false if fail.
Definition at line 262 of file RootInputFile.cc.
00263 { 00264 if (!m_file) { 00265 log << MSG::DEBUG 00266 << "openning " << m_filename << " for " << m_treepath 00267 << endreq; 00268 00269 m_file = TFile::Open(m_filename.c_str(),"READ"); 00270 } 00271 if (!m_tree) { 00272 std::string treepath = m_treepath; 00273 // Catch top-level objects 00274 if(treepath.rfind("/") == 0) treepath = treepath.substr(1); 00275 TObject* obj = m_file->Get(treepath.c_str()); 00276 if (!obj) { 00277 log << MSG::ERROR << "open(): No such object at " << m_treepath 00278 << " in " << m_filename << endreq; 00279 return false; 00280 } 00281 m_tree = dynamic_cast<TTree*>(obj); 00282 if (!m_tree) { 00283 log << MSG::ERROR 00284 << "open(): Object at " << m_treepath 00285 << " in " << m_filename 00286 << " not a TTree\n"; 00287 return false; 00288 } 00289 if (m_entries < 0) { 00290 m_entries = m_tree->GetEntries(); 00291 } 00292 } 00293 if (!m_tree) return false; 00294 00295 handle_user_data(m_tree,m_treepath); 00296 00297 return true; 00298 }
bool RootInputFile::leave | ( | ) |
Stream no longer visit this file.
Definition at line 300 of file RootInputFile.cc.
00301 { 00302 log << MSG::DEBUG 00303 << "leaving " << m_filename << " for " << m_treepath 00304 << endreq; 00305 00306 // The opened file must be closed to avoid memory leak 00307 if(m_file) { 00308 m_file->Close(); 00309 delete m_file; 00310 m_file = 0; 00311 } 00312 m_tree = 0; 00313 m_addr = 0; 00314 m_entry = -1; 00315 00316 return true; 00317 }
int RootInputFile::entry | ( | ) |
Return current entry, -1 if no entry yet set.
Definition at line 319 of file RootInputFile.cc.
00320 { 00321 return m_entry; 00322 }
bool RootInputFile::setEntry | ( | int | entry | ) |
Set entry (but no GetEntry()), return false if illegal value.
Definition at line 324 of file RootInputFile.cc.
00325 { 00326 if (!this->open()) return false; 00327 if (entry >= m_entries) { 00328 log << MSG::ERROR << "setEntry("<<entry<<"): Entry too large" 00329 << endreq; 00330 return false; 00331 } 00332 m_entry = entry; 00333 return true; 00334 }
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 355 of file RootInputFile.cc.
00356 { 00357 int nbytes = 0; 00358 return this->read(addr,nbytes); 00359 }
bool RootInputFile::read | ( | void * | addr, | |
int & | nbytes | |||
) |
Read as above, fill nbytes with number of bytes written.
Definition at line 361 of file RootInputFile.cc.
00362 { 00363 nbytes = 0; 00364 if (!this->open()) return false; 00365 if (m_entry < 0 || m_entry >= m_entries) { 00366 log << MSG::ERROR << "read(void*): Bad entry: " << m_entry << endreq; 00367 return false; 00368 } 00369 00370 if (!this->setAddr(addr)) return false; 00371 00372 log << MSG::DEBUG 00373 << "read("<<(void*)addr<<") entry=" << m_entry 00374 << " from file " << m_filename 00375 << " and branch " << m_branchname 00376 << endreq; 00377 nbytes = m_tree->GetEntry(m_entry); 00378 return nbytes > 0; 00379 }
bool RootInputFile::setAddr | ( | void * | addr | ) |
Set address of pointer to object to copy branch name into on read().
Definition at line 337 of file RootInputFile.cc.
00338 { 00339 if (addr == m_addr) return true; 00340 TBranch* b = m_tree->GetBranch(m_branchname.c_str()); 00341 if (!b) { 00342 log << MSG::ERROR << "setAddr(): no such branch named " 00343 << m_branchname << endreq; 00344 return false; 00345 } 00346 00347 log << MSG::DEBUG 00348 << "setAddr("<<(void*)addr<<") for " << m_branchname << " in " << m_filename 00349 << endreq; 00350 b->SetAddress(addr); 00351 m_addr = addr; 00352 return true; 00353 }
int RootInputFile::entries | ( | ) |
Return total number of entries.
Definition at line 381 of file RootInputFile.cc.
00382 { 00383 if (m_entries < 0) { // never opened this file 00384 if (!this->open()) return 0; 00385 this->leave(); // may not want to leave open long term, leave it as found it 00386 } 00387 return m_entries; 00388 }
bool RootInputFile::next | ( | int | steps = 1 |
) |
Advance entry number by given number of steps, return false if too far.
Definition at line 390 of file RootInputFile.cc.
bool RootInputFile::beginning | ( | ) |
Go to first entry.
Definition at line 395 of file RootInputFile.cc.
00396 { 00397 if (!this->open()) return false; 00398 m_entry = 0; 00399 return true; 00400 }
bool RootInputFile::ending | ( | ) |
Go to last entry.
Definition at line 401 of file RootInputFile.cc.
00402 { 00403 if (!this->open()) return false; 00404 m_entry = m_entries-1; 00405 return true; 00406 }
bool RootInputFile::prev | ( | int | steps = 1 |
) |
Retard entry number by given number of steps, return false if too far.
Definition at line 409 of file RootInputFile.cc.
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.
TFile* RootInputFile::m_file [private] |
Definition at line 30 of file RootInputFile.h.
TTree* RootInputFile::m_tree [private] |
Definition at line 31 of file RootInputFile.h.
int RootInputFile::m_entry [private] |
Definition at line 32 of file RootInputFile.h.
int RootInputFile::m_entries [private] |
Definition at line 32 of file RootInputFile.h.
void* RootInputFile::m_addr [private] |
Definition at line 33 of file RootInputFile.h.
Dyb::MsgStreamMember RootInputFile::log [private] |
Definition at line 35 of file RootInputFile.h.