NtupleReader/src/MyTextFileReader.cxx

00001 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00002 // 10.12.2006, AUTHOR: STEFFEN KAISER
00003 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00004 
00005 //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00006 //:: IMPLEMENTATION OF METHODS DEFINED IN THE CLASS MyTextFileReader ::
00007 //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00008 
00009 //::::::::::::::::::
00010 //:: HEADER FILES ::
00011 //::::::::::::::::::
00012 
00013 #include "NtupleReader/MyTextFileReader.h"
00014 
00015 
00016 //::::::::::::::::::::::::
00017 //:: NAMESPACE SETTINGS ::
00018 //::::::::::::::::::::::::
00019 
00020 using namespace std;
00021 
00022 
00023 //*****************************************************************************
00024 
00025 //:::::::::::::::::
00026 //:: METHOD init ::
00027 //:::::::::::::::::
00028 
00029 void MyTextFileReader::init(TString textfilename) {
00030   
00031     m_textfilename = textfilename;
00032     return;
00033 }
00034 
00035 //*****************************************************************************
00036 
00037 //:::::::::::::::::::::
00038 //:: METHOD destruct ::
00039 //:::::::::::::::::::::
00040 
00041 void MyTextFileReader::destruct(void) {
00042     
00043     return;
00044     
00045 }
00046 
00047 //*****************************************************************************
00048 
00049 //:::::::::::::::::::::::::::::
00050 //:: METHOD get_textfilename ::
00051 //:::::::::::::::::::::::::::::
00052 
00053 
00054 TString 
00055 MyTextFileReader::get_textfilename(void) {
00056 
00057     return m_textfilename;
00058 
00059 }
00060 
00061 //*****************************************************************************
00062 
00063 //::::::::::::::::::::::::::::::::::::::::
00064 //:: TEMPLATE METHOD retrieve_from_file ::
00065 //::::::::::::::::::::::::::::::::::::::::
00066 
00067 template <class MyType> MyType
00068 MyTextFileReader::retrieve_from_file(TString key_string, TString filename){
00069 
00070     // variables // 
00071 
00072     MyType value;
00073     TString aBuffer;
00074     TString dummy;
00075     
00076     TString m_filename = filename;
00077     
00078     // if no filename is given read the default (global) file //
00079   
00080     if(m_filename=="")  
00081         m_filename = m_textfilename;
00082   
00083     ifstream myFile(m_filename);
00084     
00085     // exit if file can't be opened //
00086 
00087     if (myFile.fail()){
00088         std::cerr << std::endl
00089                   << "Error: Could not open " << m_filename
00090                   << std::endl;
00091         exit(1);
00092     }
00093 
00094     // loop over lines and read value after 'key_string = ' //
00095 
00096     while ( !myFile.eof() ){
00097     
00098         aBuffer.ReadToken( myFile );
00099         //if ( aBuffer.Index(key_string) != kNPOS ){
00100         if ( aBuffer==key_string){
00101             myFile >> dummy;
00102             myFile >> value;
00103         }
00104         
00105         if (myFile.eof()) {
00106             break;
00107         }
00108     
00109     }
00110   
00111   
00112     return value;
00113 }
00114 
00115 //*****************************************************************************
00116 
00117 //:::::::::::::::::::::::
00118 //:: METHOD get_string ::
00119 //:::::::::::::::::::::::
00120 
00121 TString 
00122 MyTextFileReader::get_string(TString key_string){
00123 
00124     // the template method above can only be called from an other class //
00125     // if this method is in this class -> very strange!                  //   
00126 
00127     TString string;
00128     string = retrieve_from_file<TString>(key_string);
00129   
00130     return string;
00131   
00132 }
00133 
00134 Bool_t 
00135 MyTextFileReader::get_bool(TString key_string){
00136 
00137     // the template method above can only be called from an other class //
00138     // if this method is in this class -> very strange!                  //   
00139 
00140     Bool_t flag;
00141     flag = retrieve_from_file<Bool_t>(key_string);
00142   
00143     return flag;
00144   
00145 }
00146 
00147 int 
00148 MyTextFileReader::get_int(TString key_string){
00149 
00150     // the template method above can only be called from an other class //
00151     // if this method is in this class -> very strange!                  //   
00152 
00153     int flag;
00154     flag = retrieve_from_file<int>(key_string);
00155   
00156     return flag;
00157   
00158 }
00159 
00160 
00161 
00162 
00163 //*****************************************************************************
00164 
00165 //::::::::::::::::::::::::::::::
00166 //:: METHOD fill_cutvalue_map ::
00167 //::::::::::::::::::::::::::::::
00168 
00169 void 
00170 MyTextFileReader::fill_cutvalue_map(TString cutstring, TString filename){
00171     m_cutvalue_map.clear();
00172     // variables //
00173 
00174     TString sdummy;
00175     TString eqdummy;
00176     Double_t vdummy;
00177   
00178     int nvalues = -1;
00179     TString aBuffer;
00180   
00181     TString m_filename = filename;
00182   
00183     // if no filename is given read the default (global) file // 
00184 
00185     if(m_filename=="")  
00186         m_filename = m_textfilename;
00187   
00188     ifstream myFile(m_filename);
00189   
00190     // exit if file can't be opened //
00191 
00192     if (myFile.fail()){
00193         std::cerr << std::endl
00194                   << "Error: Could not open " << m_filename
00195                   << std::endl;
00196         exit(1);
00197     }
00198   
00199     // loop over lines and fill cutvalues and names in map     //
00200     // between 'cutstring:' and 'end_of_cutstring' in textfile //
00201   
00202     while ( !myFile.eof() ){
00203     
00204         if ( nvalues < 0 ){
00205       
00206             aBuffer.ReadToken( myFile );
00207             if ( aBuffer.Index(cutstring+":") != kNPOS ){ 
00208                 nvalues = 0;
00209             }
00210         }
00211         else{
00212    
00213             myFile >> sdummy;
00214             if(sdummy=="#end_of_"+cutstring) break;
00215             
00216             if(sdummy.Contains("#") || sdummy==""){ 
00217                 continue;
00218             }
00219 
00220             myFile >> eqdummy;
00221             myFile >> vdummy;
00222       
00223 //            cout << sdummy << "\t" << eqdummy << "\t" << vdummy << endl;
00224             
00225             m_cutvalue_map.insert(ValuePair(sdummy,vdummy));
00226 
00227             nvalues++;
00228       
00229         }
00230     
00231         if (myFile.eof()) {
00232             break;
00233         }
00234     
00235     }
00236 
00237 //     // print values stored in the map // 
00238 // 
00239  //    std::cout << "\n**********************************************" << std::endl;
00240 //     std::cout << "** in MyTextFileReader::fill_cutvalue_map() " 
00241 //            << std::endl;
00242 //     std::cout << "** cutstring: " << cutstring 
00243 //            << std::endl;
00244 //     std::cout << "** cutvalues in m_cutvalue_map:             " 
00245 //            << std::endl;
00246     
00247 //     maptype::const_iterator it;
00248 //     for( it=m_cutvalue_map.begin(); it!=m_cutvalue_map.end(); it++ ){
00249         
00250 //      std::cout << "** value: " << (*it).first << " : " 
00251 //                << (*it).second << std::endl;
00252         
00253 //     }
00254 //     std::cout << "**********************************************\n" << std::endl;
00255 
00256 
00257 }
00258 
00259 //*****************************************************************************
00260 
00261 //:::::::::::::::::::::::::
00262 //:: METHOD get_cutvalue ::
00263 //:::::::::::::::::::::::::
00264 
00265 
00266 Double_t 
00267 MyTextFileReader::get_cutvalue(TString variable){
00268 
00269     // return value with name 'variable' from map //
00270     // if value doesn't exists than exit          //
00271    
00272     Double_t value;
00273  
00274     maptype::const_iterator iter = m_cutvalue_map.find(variable);
00275     if( iter != m_cutvalue_map.end() ){
00276         value = (*iter).second;
00277     }
00278     else{
00279         std::cerr << std::endl
00280                   << "Error: Could not find variable: " 
00281                   <<  variable
00282                   << std::endl;
00283         exit(1);
00284     }
00285 
00286     return value;
00287 
00288 }
00289 
00290 
00291 
00292 // ifstream* 
00293 // MyTextFileReader::read_textfile(TString filename){
00294   
00295   
00296 //   ifstream file(filename);
00297 
00298 //   if (file.fail()){
00299 //     std::cerr << std::endl
00300 //            << "Error in MyTextFileReader::read_textfile(): Could not open " 
00301 //            << filename
00302 //            << std::endl;
00303 //     exit(1);
00304 //   }
00305   
00306 //   return &file;
00307 
00308 // }

Generated on Tue Oct 21 11:50:46 2008 for NtupleAnalysis by  doxygen 1.5.1