00001 #include "Mad/MadUtilities.h"
00002 #include "glob.h"
00003 #include <iostream>
00004 #include <string>
00005
00006 #include "TFile.h"
00007 #include "TChain.h"
00008 #include "TList.h"
00009 #include "TIterator.h"
00010 #include "TObject.h"
00011 #include "TRegexp.h"
00012 #include "TSystem.h"
00013 #include "TROOT.h"
00014
00015 Bool_t MadUtilities::MakeChains(const char* fileName)
00016 {
00017
00018 TChain *chain[5];
00019 chain[0] = 0;
00020 chain[1] = 0;
00021 chain[2] = 0;
00022 chain[3] = 0;
00023 chain[4] = 0;
00024
00025 TString filnom = MadUtilities::GetOneFile(fileName);
00026
00027 bool foundSR = false;
00028 bool foundMC = false;
00029 bool foundTH = false;
00030 bool foundEM = false;
00031 bool foundST = false;
00032 TFile *tmp = new TFile(filnom,"READ");
00033 TList *list = tmp->GetListOfKeys();
00034 TIterator *iter = list->MakeIterator();
00035 for(int i=0;i<list->GetSize();i++){
00036 TObject *obj = iter->Next();
00037 std::string nom(obj->GetName());
00038 if(nom=="NtpSR") foundSR=true;
00039 else if(nom=="NtpMC") foundMC=true;
00040 else if(nom=="NtpTH") foundTH=true;
00041 else if(nom=="NtpEM") foundEM=true;
00042 else if(nom=="NtpSt") foundST=true;
00043 }
00044 delete tmp;
00045
00046 if(foundSR){
00047 chain[0] = new TChain("NtpSR");
00048 chain[0]->Add(fileName,-1);
00049 std::cout << "Adding NtpSR Stream to Mad" << std::endl;
00050 }
00051 if(foundMC){
00052 chain[1] = new TChain("NtpMC");
00053 chain[1]->Add(fileName,-1);
00054 std::cout << "Adding NtpMC Stream to Mad" << std::endl;
00055 }
00056 if(foundTH){
00057 chain[2] = new TChain("NtpTH");
00058 chain[2]->Add(fileName,-1);
00059 std::cout << "Adding NtpTH Stream to Mad" << std::endl;
00060 }
00061 if(foundEM){
00062 chain[3] = new TChain("NtpEM");
00063 chain[3]->Add(fileName,-1);
00064 std::cout << "Adding NtpEM Stream to Mad" << std::endl;
00065 }
00066 if(foundST){
00067 chain[4] = new TChain("NtpSt");
00068 chain[4]->Add(fileName,-1);
00069 std::cout << "Adding NtpSt Stream to Mad" << std::endl;
00070 }
00071 if(foundSR || foundST) return true;
00072 return false;
00073 }
00074
00075 TString MadUtilities::GetOneFile(const char* infiles){
00076
00077
00078
00079 TString rval;
00080 glob_t found_files;
00081 int flags = 0;
00082 int n = glob(infiles,flags,NULL,&found_files);
00083 if(n!=0){
00084 std::cout<<"MadUtilties::GetOneFile(): error when globbing '"
00085 <<infiles<<"'"<<std::endl;
00086 return rval;
00087 }
00088 if(found_files.gl_pathc == 0){
00089 std::cout<<"MadUtilities::GetOneFile(): '"
00090 <<infiles<<"' fails to expand to any files"<<std::endl;
00091 return rval;
00092 }
00093
00094 rval = found_files.gl_pathv[0];
00095
00096 return rval;
00097
00098
00099 }
00100
00101 int MadUtilities::GetTotEntries(const char* fileName){
00102
00103 TString basename(fileName);
00104 Int_t slashpos = basename.Last('/');
00105 TString directory;
00106 if (slashpos>=0) {
00107 directory = basename(0,slashpos);
00108 basename.Remove(0,slashpos+1);
00109 }
00110 else {
00111 directory = gSystem->WorkingDirectory();
00112 }
00113 const char *file;
00114 int ent=0;
00115 void *dir=gSystem->OpenDirectory(gSystem->ExpandPathName(directory.Data()));
00116 if (dir) {
00117 TRegexp re(basename,kTRUE);
00118 while ((file = gSystem->GetDirEntry(dir))) {
00119 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
00120 TString s = file;
00121 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
00122 TFile *tmp = new TFile(directory+"/"+file,"READ");
00123 TTree *tree = (TTree*) tmp->Get("DaqSnarl");
00124 ent+=tree->GetEntries();
00125 delete tmp;
00126 }
00127 }
00128 return ent;
00129 }