pdscalibdata/src/CsPadPedestalsV1.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: CsPadPedestalsV1.cpp 11788 2016-04-27 17:04:41Z dubrovin@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Class CsPadPedestalsV1...
00007 //
00008 // Author List:
00009 //      Andrei Salnikov
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "pdscalibdata/CsPadPedestalsV1.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 #include <algorithm>
00022 #include <stdexcept>
00023 #include <fstream>
00024 
00025 //-------------------------------
00026 // Collaborating Class Headers --
00027 //-------------------------------
00028 #include "MsgLogger/MsgLogger.h"
00029 
00030 //-----------------------------------------------------------------------
00031 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00032 //-----------------------------------------------------------------------
00033 
00034 //              ----------------------------------------
00035 //              -- Public Function Member Definitions --
00036 //              ----------------------------------------
00037 
00038 namespace pdscalibdata {
00039 
00040 //----------------
00041 // Constructors --
00042 //----------------
00043 CsPadPedestalsV1::CsPadPedestalsV1 ()
00044 {
00045   // fill all pedestals with zeros
00046   std::fill_n(m_pedestals, int(Size), pedestal_t(0));
00047 }
00048 
00049 CsPadPedestalsV1::CsPadPedestalsV1 (const std::string& fname) 
00050 { 
00051   // open file
00052   std::ifstream in(fname.c_str());
00053   if (not in.good()) {
00054     const std::string msg = "Failed to open pedestals file: "+fname;
00055     MsgLogRoot(error, msg);
00056     throw std::runtime_error(msg);
00057   }
00058 
00059   // read all numbers
00060   pedestal_t* it = m_pedestals;
00061   size_t count = 0;
00062 
00063   //===========================
00064   /*
00065   while(in and count != Size) {
00066     in >> *it++;
00067     ++ count;
00068   }
00069   */
00070   //===========================
00071 
00072   pedestal_t  val;
00073   std::string str; 
00074   while(getline(in,str)) { 
00075         // 1. skip lines with comments (# in the 1st position)
00076         if(str[0] == '#') continue;
00077 
00078         // 2. skip empty lines 
00079         else if (str.find_first_not_of(" ")==std::string::npos) continue; 
00080 
00081         // 3. parse one line
00082         else {
00083                std::stringstream ss(str);
00084                while (ss >> val and count != Size) { 
00085                  *it++ = val;
00086                  ++ count;
00087                }
00088         }
00089   }
00090 
00091   //===========================
00092 
00093   // check that we read whole array
00094   if (count != Size) {
00095     const std::string msg = "Pedestals file does not have enough data: "+fname;
00096     MsgLogRoot(error, msg);
00097     throw std::runtime_error(msg);
00098   }
00099 
00100   // and no data left after we finished reading
00101   float tmp ;
00102   if ( in >> tmp ) {
00103     ++ count;
00104     const std::string msg = "Pedestals file has extra data: "+fname;
00105     MsgLogRoot(error, msg);
00106     MsgLogRoot(error, "read " << count << " numbers, expecting " << Size );
00107     throw std::runtime_error(msg);
00108   }
00109 }
00110 
00111 //--------------
00112 // Destructor --
00113 //--------------
00114 CsPadPedestalsV1::~CsPadPedestalsV1 ()
00115 {
00116 }
00117 
00118 } // namespace pdscalibdata

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7