Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:46:11

0001 #ifndef GUARD_surveypxbimagereader_h
0002 #define GUARD_surveypxbimagereader_h
0003 
0004 #include "Alignment/SurveyAnalysis/interface/SurveyPxbImage.h"
0005 
0006 #include <algorithm>
0007 #include <iostream>
0008 #include <sstream>
0009 #include <vector>
0010 #include <fstream>
0011 #include <string>
0012 
0013 //! Class to hold one picture of the BPix survey
0014 template <class T>
0015 class SurveyPxbImageReader {
0016 public:
0017   typedef std::vector<T> measurements_t;
0018 
0019   // Constructors
0020   //! Empty default constructor
0021   SurveyPxbImageReader(){};
0022   //! Constructor with ifstream and destination vector
0023   SurveyPxbImageReader(std::ifstream &infile, measurements_t &measurements, SurveyPxbImage::count_t reserve = 800) {
0024     read(infile, measurements, reserve);
0025   };
0026 
0027   //! Constructor with filename and destination vector
0028   SurveyPxbImageReader(std::string filename, measurements_t &measurements, SurveyPxbImage::count_t reserve = 800) {
0029     std::ifstream infile(filename.c_str());
0030     if (!infile) {
0031       std::cerr << "Cannot open file " << filename << " - operation aborted." << std::endl;
0032     }
0033     read(infile, measurements, reserve);
0034   };
0035 
0036   //! Reads a file, parses its content and fills the data vector
0037   //! All data after a hash sign (#) is treated as a comment and not read
0038   //! \param filename Filename of the file to be read
0039   //! \param measurements Vector containing the measurements, previous content will be deleted
0040   //! \param reserve Initial size of the vector, set with vector::reserve()
0041   //! \return number of succesfully read entries
0042   SurveyPxbImage::count_t read(std::ifstream &infile,
0043                                measurements_t &measurements,
0044                                SurveyPxbImage::count_t reserve = 830) {
0045     // prepare the measurements vector
0046     measurements.clear();
0047     measurements.reserve(reserve);
0048 
0049     // container for the current line
0050     std::string aLine;
0051 
0052     // loop over lines of input file
0053     while (std::getline(infile, aLine)) {
0054       // strip off everything after a hash
0055       std::string stripped = "";
0056       std::string::iterator iter = std::find(aLine.begin(), aLine.end(), '#');
0057       std::copy(aLine.begin(), iter, std::back_inserter(stripped));
0058       // read one measurment and add to vector if successfull
0059       std::istringstream iss(stripped, std::istringstream::in);
0060       T curMeas(iss);
0061       if (curMeas.isValid()) {
0062         measurements.push_back(curMeas);
0063       }
0064     }
0065 
0066     return measurements.size();
0067   }
0068 
0069 protected:
0070 };
0071 
0072 #endif