#include "eff_utils/EffTool.hpp" #include "eff_utils/EffInfo.hpp" #include "eff_utils/Efficiency.hpp" #include "eff_utils/BinnedEfficiency.hpp" #include "eff_utils/ParameterizedEfficiency.hpp" #include "TSystem.h" #include using namespace std; using std::ios; using namespace eff_utils; EffTool::~EffTool() { delete _eff; } bool EffTool::_MakeEfficiencyObj(const EffInfo& request, const std::string& path){ // add spc files from directory path and all subdirectories to the file list vector files ; AddFiles2List(path.c_str(), files) ; if (files.size() == 0) { cout << "eff_util::EffObj ERROR: Directory " << path << " does not contains any spc file!" << endl ; exit (1) ; } // Create a file_name from params. First try without converting to the lower register string FileName = request.MakeFileName(false); vector::const_iterator it = files.begin() ; for (; it != files.end(); it++) if(it->size() >= FileName.size() && it->substr(it->size()-FileName.size()) == FileName) break ; string FileName1 = FileName ; if (it == files.end()) { // now try to convert in lower register some of the meta-data strings FileName = request.MakeFileName(true); it = files.begin() ; for (; it != files.end(); it++) if(it->size() >= FileName.size() && it->substr(it->size()-FileName.size()) == FileName) break ; } if (it == files.end()) { cout << "Files: " << FileName << " " << FileName1 << endl ; throw runtime_error("eff_util ERROR: these files are not found \n [" + FileName1 + "] and [" + FileName + "] \n " + " Verify that all following fields are correctly specified: \n" + "EffName, ObjType, ObjQuality, EffVarNames ,EffType") ; } // Open the file as a ifstream object, Take care of exceptions if (FileName != "NOFILENAME") { ifstream fstr; fstr.open(it->c_str(),ios::in); string type; try { type = request.EffType(); if( type == "Binned" ) { _eff = new BinnedEfficiency( fstr, &request ); } else if( type == "Parameterized") _eff = new ParameterizedEfficiency( fstr, &request ); else _eff = 0; } catch( const EffRequestNotFoundInFile& error) { cout << "eff_utill::EffTool exception catch: " << error._message << endl << "==================================================" << endl << request << "==================================================" << endl << it->c_str() << endl ; return false ; } // now see if we can find another one! // this is kind of an evil use of exceptions since we are relying on an exception happening in cases of there being no error! // a better way might be to add some state to the efficiency whereby the Efficiency object can tell you if it contains anything // if its empty then nothing was found. Efficiency * tmpEff; try{ if( type == "Binned" ) tmpEff = new BinnedEfficiency( fstr, &request ); else if( type == "Parameterized") tmpEff = new ParameterizedEfficiency( fstr, &request ); cout << it->c_str() << endl; // if there is no further matching efficiency in the file then an exception should have been thrown and we should never reach the next line throw EffDuplicateMatchesToRequest(); } catch( const EffRequestNotFoundInFile & error) { // this is actually what we were hoping for // check that the eff found is valid and return the appropriate bool if( _eff) return _eff->isValid(); else { return false;} } // if we get here there is a problem.... return false; // file closes when it goes out of scope } // if( _eff && _eff->isValid()) { //if (request != _eff->Info()) // cout << "eff_utils::EffTool Warning: The requested meta data are less specific than the found one!" // << endl << "Will use the first efficiency found in the file!" // << endl << "REQUESTED: " << request // << endl << "FOUND: " << _eff->Info() // << endl ; //return true; else return false; } bool EffTool::Info(const EffInfo& spec) { _isValid = _MakeEfficiencyObj(spec); return _isValid; } void EffTool::AddFiles2List(const char* path, vector& files) { void* work_dir = 0 ; work_dir = gSystem->OpenDirectory(path) ; if (!work_dir) { cout << "eff_util::EffTool ERROR: Directory " << path << " is not found!" << endl ; return ; } const char* file_name_c ; while ( (file_name_c = gSystem->GetDirEntry(work_dir)) != 0 ) { Long_t id = 0, flags = 0, modtime = 0 ; Long64_t size = 0 ; if (gSystem->GetPathInfo(gSystem->ConcatFileName(path,file_name_c) , &id, &size, &flags, &modtime ) != 0) continue ; string file_name = file_name_c ; if (file_name == "." || file_name == "..") continue ; if ( (flags & 2) == 2) { AddFiles2List(gSystem->ConcatFileName(path,file_name_c), files) ; continue ; } if ( file_name.find(".spc") == string::npos ) continue ; files.push_back((string) gSystem->ConcatFileName(path,file_name_c) ) ; } gSystem->FreeDirectory(work_dir) ; return ; }