Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

RootTuple.cxx

Go to the documentation of this file.
00001 //$Header: /nfs/slac/g/glast/ground/cvs/merit/src/app/RootTuple.cxx,v 1.3 2001/10/23 15:06:12 burnett Exp $
00002 // Original author T. Burnett (w/ help from H. Kelley)
00003 #include "RootTuple.h"
00004 
00005 // root includes
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     // convenient utility from Heather
00024 TTree* getTree(TFile *f) {
00025   // Create an iterator on the list of keys
00026   TIter nextTopLevelKey(f->GetListOfKeys());
00027   TKey *keyTopLevel, *curDirKey;
00028   TTree* t=0; // return 
00029 
00030   // loop on keys, and search for the TTree named "t1"
00031   while  ( keyTopLevel=(TKey*)nextTopLevelKey() ) {
00032     // I'm assuming we know the name of the TTree is "t1"
00033     TString name(keyTopLevel->GetName());
00034     TString className(keyTopLevel->GetClassName());
00035 
00036     if ((name.CompareTo("t1")==0) && (className.CompareTo("TTree")==0))  {
00037       // Found It
00038       t = (TTree*)f->Get(keyTopLevel->GetName());
00039       return t;
00040     }
00041     // If we find a directory - then we search it as well
00042     // Here I'm assuming that our directory structure only goes down one-level
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           // Found it
00051           t = (TTree*)curDir->Get(curDirKey->GetName());
00052           return t;
00053         }
00054       }
00055     }
00056   }
00057   return t;
00058 }
00059 
00060 } // anonymous namespace
00061 
00062 RootTuple::RootTuple(std::string title, std::string file, std::string treeName)
00063 : Tuple(title), m_event(0) {
00064 
00065     // Initialize Root
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     // Open the file, and get at the  TTree containing the data
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     // get the list of branches
00092     TObjArray* ta = m_tree->GetListOfBranches();
00093 
00094     // now iterate.
00095     int entries = ta->GetEntries();
00096     for( int i = 0; i<entries; ++i) { // should try a TIter
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     // setup the ntuple to access just this list of events
00125     myNtuple->SetEventList(list);
00126     
00127     myNtuple->SetEventList(0);  // will now be able to access all events again
00128     
00129     
00130     // iterate over all events
00131     for (i = 0; i < numEvents; i++) 
00132     {
00133         // setup L1T to contain the data in the L1T column of the ntuple
00134         Float_t L1T;
00135         TBranch* L1Tbranch = myNtuple->GetBranch("L1T");        
00136         L1Tbranch->SetAddress(&L1T);
00137         // retrieve the data for event i
00138         myNtuple->GetEvent(i);
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 }

Generated at Wed Nov 21 12:20:39 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000