CMS 3D CMS Logo

SprSimpleReader.cc

Go to the documentation of this file.
00001 //$Id: SprSimpleReader.cc,v 1.4 2007/12/01 01:29:46 narsky Exp $
00002 
00003 #include "PhysicsTools/StatPatternRecognition/interface/SprExperiment.hh"
00004 #include "PhysicsTools/StatPatternRecognition/interface/SprSimpleReader.hh"
00005 #include "PhysicsTools/StatPatternRecognition/interface/SprData.hh"
00006 #include "PhysicsTools/StatPatternRecognition/interface/SprAbsFilter.hh"
00007 #include "PhysicsTools/StatPatternRecognition/interface/SprEmptyFilter.hh"
00008 #include "PhysicsTools/StatPatternRecognition/interface/SprPreFilter.hh"
00009 
00010 #include <algorithm>
00011 #include <utility>
00012 #include <cassert>
00013 #include <fstream>
00014 #include <iostream>
00015 #include <sstream>
00016 
00017 using namespace std;
00018 
00019 
00020 SprSimpleReader::SprSimpleReader(int mode, SprPreFilter* filter)
00021   : 
00022   SprAbsReader(filter), 
00023   mode_(mode),
00024   include_(),
00025   exclude_()
00026 {
00027   assert( mode_>0 && mode_<8 );
00028 }
00029 
00030 
00031 SprAbsFilter* SprSimpleReader::read(const char* filename)
00032 {
00033   // cannot request and exclude variables at the same time
00034   if( !include_.empty() && !exclude_.empty() ) {
00035     cerr << "You cannot include and exclude variables at the same time." 
00036          << endl;
00037     return 0;
00038   }
00039 
00040   // open file
00041   string fname = filename;
00042   ifstream file(fname.c_str());
00043   if( !file ) {
00044     cerr << "Unable to open file " << fname.c_str() << endl;
00045     return 0;
00046   }
00047 
00048   // init
00049   string line;
00050   unsigned dim = 0;
00051   unsigned nline = 0;
00052 
00053   // read number of dimensions
00054   if( mode_ != 7 ) {
00055     while( getline(file,line) ) {
00056       nline++;
00057       if( line.find('#') != string::npos )
00058         line.erase( line.find_first_of('#') );
00059       if( line.find_first_not_of(' ') == string::npos ) continue;
00060       istringstream ist(line);
00061       ist >> dim;
00062       assert( dim != 0 );
00063       break;
00064     }
00065   }
00066 
00067   // read var names
00068   vector<int> ind;
00069   vector<string> selected;
00070   while( getline(file,line) ) {
00071     nline++;
00072     if( line.find('#') != string::npos )
00073       line.erase( line.find_first_of('#') );
00074     if( line.find_first_not_of(' ') == string::npos ) continue;
00075     istringstream ist(line);
00076     string varName;
00077     if(      mode_==5 || mode_==6 ) {// variable names take separate lines
00078       ist >> varName;
00079       if( exclude_.find(varName)==exclude_.end() &&
00080           (include_.empty() || include_.find(varName)!=include_.end()) ) {
00081         ind.push_back(selected.size());
00082         selected.push_back(varName);
00083       }
00084       if( ind.size() >= dim ) break;
00085     }
00086     else if( mode_ == 7 ) {// don't know how many vars yet
00087       int varCounter = 0;
00088       while( ist >> varName ) {
00089         if( ++varCounter > 3 ) {
00090           if( exclude_.find(varName)==exclude_.end() &&
00091               (include_.empty() || include_.find(varName)!=include_.end()) ) {
00092             ind.push_back(selected.size());
00093             selected.push_back(varName);
00094           }
00095         }
00096       }
00097       dim = selected.size();
00098       break;
00099     }
00100     else {// all variable names are on one line
00101       ind.clear();
00102       ind.resize(dim,-1);
00103       for( int i=0;i<dim;i++ ) {
00104         ist >> varName;
00105         if( exclude_.find(varName)==exclude_.end() &&
00106             (include_.empty() || include_.find(varName)!=include_.end()) ) {
00107           ind[i] = selected.size();
00108           selected.push_back(varName);
00109         }
00110       }
00111       break;
00112     }
00113   }
00114 
00115   // check if all requested input variables have been found
00116   for( set<string>::const_iterator i=include_.begin();i!=include_.end();i++ ) {
00117     if( find(selected.begin(),selected.end(),*i) == selected.end() ) {
00118       cerr << "Variable " << i->c_str() 
00119            << " has not been found in file " << fname.c_str() << endl;
00120       return 0;
00121     }
00122   }
00123 
00124   // set up filter
00125   if( filter_!=0 && !filter_->setVars(selected) ) {
00126     cerr << "Unable to apply pre-filter requirements." << endl;
00127     return 0;
00128   }
00129 
00130   // get a new list of variables
00131   vector<string> transformed;
00132   if( filter_ != 0 ) {
00133     if( !filter_->transformVars(selected,transformed) ) {
00134       cerr << "Pre-filter is unable to transform variables." << endl;
00135       return 0;
00136     }
00137   }
00138   if( transformed.empty() ) transformed = selected; 
00139 
00140   // construct a data object to hold the sample points
00141   auto_ptr<SprData> data(new SprData);
00142   if( !data->setVars(transformed) ) {
00143     cerr << "Unable to set variable list for input data." << endl;
00144     return 0;
00145   }
00146 
00147   // read in points, one by one
00148   vector<double> v(selected.size());
00149   vector<double> weights;
00150   int charge = 0;
00151   bool readcls = false;
00152   while( getline(file,line) ) {
00153     // get line
00154     nline++;
00155     if( line.find('#') != string::npos )
00156       line.erase( line.find_first_of('#') );
00157     if( line.find_first_not_of(' ') == string::npos ) continue;
00158 
00159     // read coords
00160     int icls = 0;
00161     double weight = 1.;
00162     int pointIndex = -1;
00163     istringstream ist(line);
00164     if( mode_ == 7 ) {
00165       ist >> pointIndex >> icls >> weight;
00166       assert( pointIndex >= 0 );
00167     }
00168     if( !readcls ) {
00169       for( int i=0;i<dim;i++ ) {
00170         double r = 0;
00171         ist >> r;
00172         int index = ind[i];
00173         if( index >= 0 ) v[index] = r;
00174       }
00175       if( mode_ == 3 ) ist >> charge;
00176     }
00177     if( mode_ == 4 || mode_ == 6 ) ist >> weight;
00178 
00179     // if 2 modes split into lines, skip the rest
00180     if( (mode_==2 || mode_==3) && !readcls ) {
00181       readcls = true;
00182       continue;
00183     }
00184 
00185     // read class
00186     if( mode_ != 7 ) {
00187       ist >> icls;
00188       readcls = false;
00189     }
00190 
00191     // assign class for special modes
00192     if( mode_ == 3 ) {
00193       icls = ( icls<=0 ? -1 : 1 );
00194       icls = ( (icls*charge)<0 ? 0 : 1);
00195     }
00196 
00197     // passes selection requirements?
00198     if( filter_!=0 && !filter_->pass(icls,v) ) continue;
00199 
00200     // compute user-defined class
00201     if( filter_!=0 ) {
00202       pair<int,bool> computedClass = filter_->computeClass(v);
00203       if( computedClass.second ) 
00204         icls = computedClass.first;
00205     }
00206 
00207     // transform coordinates
00208     if( filter_ != 0 ) {
00209       vector<double> vNew;
00210       if( filter_->transformCoords(v,vNew) ) {
00211         if( mode_ == 7 )
00212           data->insert(pointIndex,icls,vNew);
00213         else
00214           data->insert(icls,vNew);
00215       }
00216       else {
00217         cerr << "Pre-filter is unable to transform coordinates." << endl;
00218         return 0;
00219       }
00220     }
00221     else {
00222       if( mode_ == 7 )
00223         data->insert(pointIndex,icls,v);
00224       else
00225         data->insert(icls,v);
00226     }
00227 
00228     // store weight
00229     if( mode_==4 || mode_==6 || mode_==7 ) weights.push_back(weight);
00230   }
00231 
00232   // exit
00233   if( mode_ == 4 || mode_ == 6 || mode_==7 )
00234     return new SprEmptyFilter(data.release(), weights, true);
00235   return new SprEmptyFilter(data.release(), true);
00236 }
00237 

Generated on Tue Jun 9 17:42:03 2009 for CMSSW by  doxygen 1.5.4