CMS 3D CMS Logo

SprAsciiWriter.cc

Go to the documentation of this file.
00001 //$Id: SprAsciiWriter.cc,v 1.2 2007/09/21 22:32:09 narsky Exp $
00002 
00003 #include "PhysicsTools/StatPatternRecognition/interface/SprExperiment.hh"
00004 #include "PhysicsTools/StatPatternRecognition/interface/SprAsciiWriter.hh"
00005 
00006 #include <stdlib.h>
00007 #include <string>
00008 #include <iostream>
00009 #include <errno.h>
00010 #include <sys/types.h>
00011 #include <sys/stat.h>
00012 #include <stdio.h>
00013 
00014 using namespace std;
00015 
00016 
00017 bool SprAsciiWriter::init(const char* filename)
00018 {
00019   // init
00020   string fname = filename;
00021   string cmd;
00022 
00023   // check if file exists, delete and issue a warning
00024   struct stat buf;
00025   if( stat(fname.c_str(),&buf) == 0 ) {
00026     cerr << "Warning: file " << fname.c_str() << " will be deleted." << endl;
00027     cmd = "rm -f ";
00028     cmd += fname.c_str();
00029     if( system(cmd.c_str()) != 0 ) {
00030       cerr << "Attempt to delete file " << fname.c_str() 
00031            << " terminated with error " << errno << endl;
00032       return false;
00033     }
00034   }
00035 
00036   // open output stream
00037   outfile_.open(fname.c_str());
00038   if( !outfile_ ) {
00039     cerr << "Cannot open file " << fname.c_str() << endl;
00040     return false;
00041   }
00042 
00043   // exit
00044   return true;
00045 }
00046 
00047 
00048 bool SprAsciiWriter::write(int cls, unsigned index, double weight,
00049                            const std::vector<double>& v, 
00050                            const std::vector<double>& f)
00051 {
00052   // sanity check
00053   int vdim = v.size();
00054   int fdim = f.size();
00055   if( (vdim+fdim) != axes_.size() ) {
00056     cerr << "Dimensionality of input vector unequal to dimensionality " 
00057          << "of tuple: " << vdim << " " << fdim
00058          << " " << axes_.size() << endl;
00059     return false;
00060   }
00061 
00062   // write data variables and classifier names
00063   if( firstCall_ ) {
00064     firstCall_ = false;
00065     {
00066       char s [200];
00067       sprintf(s," %10s ","index");
00068       outfile_ << s;
00069     }
00070     {
00071       char s [200];
00072       sprintf(s," %10s ","i");
00073       outfile_ << s;
00074     }
00075     {
00076       char s [200];
00077       sprintf(s," %10s ","w");
00078       outfile_ << s;
00079     }
00080     for( int i=0;i<axes_.size();i++ ) {
00081       char s [200];
00082       sprintf(s," %20s ",axes_[i].c_str());
00083       outfile_ << s;
00084     }
00085     outfile_ << endl;
00086   }
00087 
00088   // write output
00089   {
00090     char s [200];
00091     sprintf(s," %10i ",index);
00092     outfile_ << s;
00093   }
00094   {
00095     char s [200];
00096     sprintf(s," %10i ",cls);
00097     outfile_ << s;
00098   }
00099   {
00100     char s [200];
00101     sprintf(s," %10g ",weight);
00102     outfile_ << s;
00103   }
00104   for( int i=0;i<vdim;i++ ) {
00105     char s [200];
00106     sprintf(s,"           %10g ",v[i]);
00107     outfile_ << s;
00108   }
00109   for( int i=0;i<fdim;i++ ) {
00110     char s [200];
00111     sprintf(s,"           %10g ",f[i]);
00112     outfile_ << s;
00113   }
00114   outfile_ << endl;
00115 
00116   // exit
00117   return true;
00118 }
00119 
00120 
00121 bool SprAsciiWriter::close()
00122 {
00123   outfile_.close();
00124   if( !outfile_ ) {
00125     cerr << "Unable to close output file." << endl;
00126     return false;
00127   }
00128   return true;
00129 }
00130 
00131 

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