#include #include #include "TTree.h" #include "TChain.h" #include "TFile.h" #include "CChainGetter.h" using namespace std; bool CChainGetter::CheckFile(string filename) { bool flag=false; { fstream fin; fin.open(filename.c_str(),ios::in); if( fin.is_open() ) { flag=true; } fin.close(); } return flag; } bool CChainGetter::CheckTree(string filename) { bool flag=false; if(!CheckFile(filename.c_str())) { return false; } TTree *pTree=GetTree(filename.c_str()); if(pTree) { flag=true; } return flag; } int CChainGetter::ReadFileNames() { int nFiles=0; //________________________________________ //Read all lines in filename and store them in vFileNames if(m_strFileListFile.empty()) { return nFiles; } ifstream ifs( m_strFileListFile.c_str() ); string temp; while( getline( ifs, temp ) ) { temp=temp+m_strFileExtension; //Add to list if file exists, only bool flag = false; flag=CheckTree(temp.c_str()); if(flag) { m_vFileNames.push_back( temp ); nFiles++; } else { cout << "Warning File: " << temp.c_str() << " -> check failed!" << endl; } } vector::const_iterator cit; for(cit=m_vFileNames.begin();cit!=m_vFileNames.end();cit++) { printf("%s\n", (*cit).c_str()); } return nFiles; } TTree * CChainGetter::GetTree(TString SingleFilename) { TTree *pTree=NULL; if( !SingleFilename. Length()== 0 ) { //printf( "Using: %s\n", SingleFilename.Data() ); TFile *pFile=new TFile(SingleFilename, "READ"); if(pFile) { pTree = (TTree*)pFile->Get(m_strTreeName.c_str()); } } } TChain* CChainGetter::GetChain(TString SingleFilename) { TChain *pTree=NULL; if( !SingleFilename. Length()== 0 ) { printf( "Using: %s\n", SingleFilename.Data() ); TFile *pFile=new TFile(SingleFilename, "READ"); assert(pFile); (TTree*)pTree = (TTree*)pFile->Get(m_strTreeName.c_str()); } else { //________________________________________ //Use Files listed in the file specified by gcstrFileListFile printf( "Using: %s\n", m_strFileListFile.c_str() ); if( ReadFileNames() ) { pTree=new TChain( m_strTreeName.c_str() ); assert(pTree); vector::const_iterator citFile; for(citFile=m_vFileNames.begin();citFile!=m_vFileNames.end();citFile++) { pTree->Add(citFile->c_str()); } } } return pTree; }