Anp::ReadTree< T > Class Template Reference

#include <ReadTree.h>

List of all members.

Classes

struct  Data

Public Member Functions

 ReadTree ()
 ~ReadTree ()
int Find (std::string path, std::string tree, int nfile=0)
int GetNFiles () const
const std::string GetFilePath (int ifile) const
const std::string GetFileName (int ifile) const
const std::vector< T > ReadFile (int ifile, std::string tname, std::string bname="") const
void Clear ()
long int GetNEntries () const

Private Attributes

std::string fTreeName
long int fNEntries
std::vector< DatafDataVec

Detailed Description

template<typename T>
class Anp::ReadTree< T >

Definition at line 24 of file ReadTree.h.


Constructor & Destructor Documentation

template<typename T >
Anp::ReadTree< T >::ReadTree (  )  [inline]

Definition at line 68 of file ReadTree.h.

00068 :fTreeName(""), fNEntries(0) {}

template<typename T >
Anp::ReadTree< T >::~ReadTree (  )  [inline]

Definition at line 72 of file ReadTree.h.

00072 {}


Member Function Documentation

template<typename T >
void Anp::ReadTree< T >::Clear ( void   )  [inline]

Definition at line 272 of file ReadTree.h.

References Anp::ReadTree< T >::fDataVec, Anp::ReadTree< T >::fNEntries, and Anp::ReadTree< T >::fTreeName.

00273 {
00274    fDataVec.clear();
00275    fNEntries = 0;
00276    fTreeName.clear();
00277 }

template<typename T >
int Anp::ReadTree< T >::Find ( std::string  path,
std::string  tree,
int  nfile = 0 
) [inline]

Definition at line 76 of file ReadTree.h.

References Anp::RunFinder::AddDir(), Anp::ReadTree< T >::fDataVec, find(), fname, Anp::ReadTree< T >::fNEntries, Anp::RunFinder::GetFile(), Anp::RunFinder::GetFileSize(), Anp::RunFinder::GetNFiles(), Anp::RunFinder::GetPath(), Anp::ReadTree< T >::Data::name, Anp::ReadTree< T >::Data::nentries, and Anp::ReadTree< T >::Data::path.

00077 {
00078    //
00079    // Search directory "path" for all root files that contain TTree "tree",
00080    // if tree string is not empty.
00081    //
00082    
00083    if(path.empty())
00084    {
00085       std::cerr << "Anp::ReadTree<T>::Find -  path is empty" << std::endl;
00086       return 0;
00087    }
00088    
00089    RunFinder finder;
00090    
00091    finder.AddDir(path, ".root");   
00092    if(finder.GetNFiles() < 1)
00093    {
00094       std::cerr << "Anp::ReadTree<T>::Find - failed to find any ROOT files in " << std::endl
00095                 << "   " << path << std::endl;
00096       return 0;
00097    }
00098    
00099    long int nrecord = 0, ifile = 0;
00100    double fsize = 0.0;
00101    
00102    for(int i = 0; i < finder.GetNFiles(); ++i)
00103    {
00104       const std::string &fpath = finder.GetPath(i);
00105       const std::string &fname = finder.GetFile(i);
00106 
00107       Data data;
00108       data.path = fpath;
00109       data.name = fname;
00110 
00111       if(!tree.empty())
00112       {
00113          TFile file(fpath.c_str(), "READ");
00114          if(!file.IsOpen())
00115          {
00116             std::cerr << "Anp::ReadTree<T>::Find - failed to open ROOT file:" << std::endl
00117                       << "   " << fpath << std::endl;
00118             continue;
00119          }
00120 
00121          TTree *rtree = dynamic_cast<TTree *> (file.Get(tree.c_str()));
00122          if(!rtree)
00123          {
00124             std::cerr << "Anp::ReadTree<T>::Find - failed to find TTree " << tree << " in ROOT file:" 
00125                       << std::endl << "   " << fpath << std::endl;
00126             continue;
00127          }
00128 
00129          data.nentries = rtree -> GetEntries();
00130       }     
00131       
00132       if(std::find(fDataVec.begin(), fDataVec.end(), data) != fDataVec.end())
00133       {
00134          std::cerr << "Anp::ReadTree<T>::Find - ignoring already existing file:" << std::endl 
00135                    << "   " << fpath << std::endl;
00136          continue;
00137       }
00138       
00139       fNEntries += data.nentries;
00140       nrecord += data.nentries;
00141       fsize += finder.GetFileSize(i);
00142       ++ifile;
00143       
00144       fDataVec.push_back(data);
00145 
00146       if(nfile > 0 && ifile == nfile)
00147       {
00148          break;
00149       }
00150    }
00151 
00152    std::cout << "Anp::ReadTree<T>::Find - search summary:" << std::endl
00153              << "   Number of files = " << ifile << std::endl
00154              << "   Size of files = " << fsize << " MB" << std::endl
00155              << "   Number of records = " << nrecord << std::endl;
00156 
00157    return ifile;
00158 }

template<typename T >
const std::string Anp::ReadTree< T >::GetFileName ( int  ifile  )  const [inline]

Definition at line 182 of file ReadTree.h.

References Anp::ReadTree< T >::fDataVec.

00183 {
00184    if(ifile < 0 || ifile >= static_cast<int>(fDataVec.size()))
00185    {
00186       std::cerr << "Anp::ReadTree<T>::GetFileName - file index is out of range: " << ifile << std::endl;
00187       return std::string("");
00188    }
00189    
00190    return fDataVec[ifile].name;
00191 }

template<typename T >
const std::string Anp::ReadTree< T >::GetFilePath ( int  ifile  )  const [inline]

Definition at line 169 of file ReadTree.h.

References Anp::ReadTree< T >::fDataVec.

00170 {
00171    if(ifile < 0 || ifile >= static_cast<int>(fDataVec.size()))
00172    {
00173       std::cerr << "Anp::ReadTree<T>::GetFilePath - file index is out of range: " << ifile << std::endl;
00174       return std::string("");
00175    }
00176    
00177    return fDataVec[ifile].path;
00178 }

template<typename T >
long int Anp::ReadTree< T >::GetNEntries (  )  const [inline]

Definition at line 281 of file ReadTree.h.

References Anp::ReadTree< T >::fNEntries.

00282 {
00283    return fNEntries;
00284 }

template<typename T >
int Anp::ReadTree< T >::GetNFiles (  )  const [inline]

Definition at line 162 of file ReadTree.h.

References Anp::ReadTree< T >::fDataVec.

00163 {
00164    return fDataVec.size();
00165 }

template<typename T >
const std::vector< T > Anp::ReadTree< T >::ReadFile ( int  ifile,
std::string  tname,
std::string  bname = "" 
) const [inline]

Definition at line 195 of file ReadTree.h.

References Anp::ReadTree< T >::fDataVec, nentries, Anp::ReadTree< T >::Data::nentries, and Anp::ReadTree< T >::Data::path.

00198 {
00199    //
00200    // Read and return all entries from ifile. This function requiries global function
00201    // with signature: Init(Tree &, T &), it can be in global or Anp namespace.
00202    //
00203 
00204    std::vector<T> tvec;
00205 
00206    if(ifile < 0 || ifile >= static_cast<int>(fDataVec.size()))
00207    {
00208       std::cerr << "Anp::ReadTree<T>::ReadFile - file index is out of range: " << ifile << std::endl;
00209       return tvec;
00210    }
00211 
00212    const Data &data = fDataVec[ifile];
00213 
00214    TFile file(data.path.c_str(), "READ");
00215    if(!file.IsOpen())
00216    {
00217       std::cerr << "Anp::ReadTree<T>::ReadFile - failed to open ROOT file" << std::endl
00218            << "   " << data.path << std::endl;
00219       return tvec;
00220    }
00221    
00222    TTree *rtree = dynamic_cast<TTree *> (file.Get(tname.c_str()));
00223    if(!rtree)
00224    {
00225       std::cerr << "Anp::ReadTree<T>::Fill - failed to find tree " << tname << std::endl;
00226       return tvec;
00227    }
00228       
00229    //
00230    // Set TTree branches
00231    //
00232    T *record = new T;
00233    if(!bname.empty())
00234    {
00235       TBranch *branch = 0;   
00236       rtree -> SetBranchAddress(bname.c_str(), &record, &branch);
00237       if(!branch)
00238       {
00239          std::cerr << "ReadTree<T>::ReadFile - branch does not exist: " << bname << std::endl;
00240          return tvec;
00241       }
00242    }
00243    else
00244    {
00245       if(!Init(*rtree, *record))
00246       {
00247          std::cerr << "Anp::ReadTree<T>::Fill - failed to initialize TTree and record" << std::endl;
00248          return tvec;      
00249       }
00250    }
00251 
00252    const int nentries = rtree -> GetEntries();
00253    if(nentries != data.nentries)
00254    {
00255       std::cerr << "Anp::ReadTree<T>::Fill - mismatched number of entries "
00256            << "previous = " << data.nentries << " and current" << data.nentries << std::endl;
00257       return tvec;
00258    }
00259 
00260    for(int i = 0; i < nentries; ++i)
00261    {
00262       rtree -> GetEntry(i);
00263       tvec.push_back(*record);
00264    }
00265    delete record;
00266 
00267    return tvec;
00268 }


Member Data Documentation

template<typename T >
std::vector<Data> Anp::ReadTree< T >::fDataVec [private]
template<typename T >
long int Anp::ReadTree< T >::fNEntries [private]
template<typename T >
std::string Anp::ReadTree< T >::fTreeName [private]

Definition at line 59 of file ReadTree.h.

Referenced by Anp::ReadTree< T >::Clear().


The documentation for this class was generated from the following file:

Generated on 2 Jul 2018 for loon by  doxygen 1.6.1