00001
00002
00003 #include "RootTuple.h"
00004
00005
00006 #include "TROOT.h"
00007 #include "TFile.h"
00008 #include "TBranch.h"
00009 #include "TEventList.h"
00010 #include "TTree.h"
00011 #include "TSystem.h"
00012 #include "TLeafF.h"
00013 #include "TIterator.h"
00014 #include "TKey.h"
00015
00016 #include "TFile.h"
00017 #include "TDirectory.h"
00018 #include "TTree.h"
00019 #include "TKey.h"
00020 #include "TIterator.h"
00021 #include "TString.h"
00022 namespace {
00023
00024 TTree* getTree(TFile *f) {
00025
00026 TIter nextTopLevelKey(f->GetListOfKeys());
00027 TKey *keyTopLevel, *curDirKey;
00028 TTree* t=0;
00029
00030
00031 while ( keyTopLevel=(TKey*)nextTopLevelKey() ) {
00032
00033 TString name(keyTopLevel->GetName());
00034 TString className(keyTopLevel->GetClassName());
00035
00036 if ((name.CompareTo("t1")==0) && (className.CompareTo("TTree")==0)) {
00037
00038 t = (TTree*)f->Get(keyTopLevel->GetName());
00039 return t;
00040 }
00041
00042
00043 if (className.CompareTo("TDirectory")==0) {
00044 TDirectory *curDir = (TDirectory*)f->Get(name);
00045 TIter dirKeys(curDir->GetListOfKeys());
00046 while ( (curDirKey = (TKey*)dirKeys() ) ) {
00047 TString name(curDirKey->GetName());
00048 TString className(curDirKey->GetClassName());
00049 if ( (name.CompareTo("t1")==0) && (className.CompareTo("TTree")==0) ) {
00050
00051 t = (TTree*)curDir->Get(curDirKey->GetName());
00052 return t;
00053 }
00054 }
00055 }
00056 }
00057 return t;
00058 }
00059
00060 }
00061
00062 RootTuple::RootTuple(std::string title, std::string file, std::string treeName)
00063 : Tuple(title), m_event(0) {
00064
00065
00066 if ( 0 == gROOT ) {
00067 static TROOT meritRoot("root","ROOT I/O");
00068 #ifdef WIN32
00069 gSystem->Load("libTree.dll");
00070 #endif
00071 }
00072
00073
00074 TFile* tfile = new TFile(file.c_str(), "read");
00075 if( tfile==0 ) {
00076 std::cerr << "file \""<< file << "\" not found." << std::endl;
00077 exit(1);
00078 }
00079 m_tree = (TTree*)tfile->Get(treeName.c_str());
00080 if( m_tree==0)
00081 m_tree = getTree(tfile);
00082 if( m_tree ==0 ) {
00083 std::cerr << "tree \""<<treeName<< "\" not found." << std::endl;
00084 tfile->ls();
00085 exit(1);
00086 }
00087
00088 m_numEvents = m_tree->GetEntries();
00089
00090 #if 0 // save this for reference
00091
00092 TObjArray* ta = m_tree->GetListOfBranches();
00093
00094
00095 int entries = ta->GetEntries();
00096 for( int i = 0; i<entries; ++i) {
00097 TBranch * b = (TBranch*)(*ta)[i];
00098 TLeafF* leaf = (TLeafF*)(*b->GetListOfLeaves())[0];
00099 const char * name = leaf->GetName();
00100 float * pf = (float*)leaf->GetValuePointer();
00101 }
00102 #endif
00103
00104
00105 }
00106
00107 const TupleItem* RootTuple::tupleItem(const std::string& name)const
00108 {
00109 Tuple::const_iterator it = find(name);
00110 if( it != end() ) return *it;
00111
00112
00113 TBranch* b = m_tree->GetBranch(name.c_str());
00114 if( b==0 ) {
00115 std::cerr << "Sorry, did not find '" << name << "' in the tuple\n";
00116 exit(-1);
00117 return *it;
00118 }
00119 TLeafF* leaf = (TLeafF*)(*b->GetListOfLeaves())[0];
00120 const char * fname = leaf->GetName();
00121 float * pf = (float*)leaf->GetValuePointer();
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 return new TupleItem(name,pf);
00142 }
00143
00144 bool RootTuple::nextEvent(){
00145 if(m_event<m_numEvents) {
00146 m_tree->GetEvent(m_event++);
00147 return true;
00148 }
00149 return false;
00150 }