emcOMLCTofFEM.C

Go to the documentation of this file.
00001 #include "emcOMAsciiT.h"
00002 #include "emcLCTofFEM.h"
00003 #include "emcDataManager.h"
00004 #include "EmcIndexer.h"
00005 #include "asciitimestamp.h"
00006 #include <string>
00007 #include <vector>
00008 #include <fstream>
00009 #include <sstream>
00010 #include "dirfilemanip.h"
00011 
00012 namespace
00013 {
00014   bool namer(const emcLCTofFEM& hlr,
00015              int to_or_from,
00016              std::string& filename)
00017   {
00018     int SN, SM144N;
00019     
00020     EmcIndexer::PXSM144_iSiSM144(hlr.AbsolutePosition(), SN, SM144N);
00021 
00022     emcDataManager* dm = emcDataManager::GetInstance();
00023 
00024     std::string dirname;
00025 
00026     if ( to_or_from == 0 )
00027       {
00028         dirname = dm->GetSourceDir();
00029       }
00030     else
00031       {
00032         dirname = dm->GetDestinationDir();
00033       }
00034 
00035     dirname += "/ToF";
00036 
00037     if ( to_or_from )
00038       {
00039         if (!createDirectory(dirname))
00040           {
00041             return false;
00042           }
00043       }
00044     
00045     std::ostringstream sfilename;
00046 
00047     sfilename << expand(dirname) << "/"
00048               << EmcIndexer::EmcSectorId(SN) << "SM"
00049               << SM144N << ".TOF_LC";
00050 
00051     filename = sfilename.str();
00052 
00053     return true;
00054   }
00055 
00056   bool writer(const emcLCTofFEM& tofFEM, int)
00057   {
00058     std::string filename;
00059     bool ok = namer(tofFEM,1,filename); 
00060     if (!ok) 
00061       {
00062         return false;
00063       }
00064     
00065     if (checkFile(filename))
00066       {
00067         std::cerr << __FILE__ << ":" << __LINE__
00068                   << " File " << filename
00069                   << " is on the way. Remove it first"
00070                   << std::endl;
00071         return false;
00072       }
00073 
00074     std::ofstream out(filename.c_str());
00075     if (!out.good())
00076       {
00077         std::cerr << __FILE__ << " Could not create file " 
00078                   << filename << std::endl;
00079         return false;
00080       }
00081 
00082     out << tofFEM.GetStartValTime() << std::endl;
00083     out << PHTimeStamp() << std::endl;
00084 
00085     for ( size_t i = 0; i < tofFEM.size(); ++i )
00086       {
00087         out << i << " " << tofFEM.getValue(i,0) << " "
00088             << tofFEM.getValue(i,1) << std::endl;
00089       }
00090 
00091     out.close();
00092 
00093     emcDataManager* dm = emcDataManager::GetInstance();
00094 
00095     if ( dm->GetVerboseLevel() )
00096       {
00097         std::cout << "emcOMLCTofFEM written to " 
00098                   << filename << std::endl;
00099       }
00100 
00101     return true;
00102   }
00103 
00104   bool reader(emcLCTofFEM& tofFEM, int code)
00105   {
00106     if ( code >= 0 )
00107       {
00108         int femAbsPosition;
00109         int idummy;
00110         emcCalFEM::FEMDecode(code,femAbsPosition,idummy,idummy,idummy);
00111         if (femAbsPosition!=tofFEM.AbsolutePosition())
00112           {
00113             std::cerr << __FILE__ << ":" << __LINE__
00114                       << " code mismatch with absolutePosition"
00115                       << std::endl;
00116             return false;
00117           }
00118       }
00119 
00120     std::string filename;
00121 
00122     bool ok = namer(tofFEM,0,filename);
00123 
00124     if (!ok)
00125       {
00126         return false;
00127       }
00128 
00129     if (!checkFile(filename))
00130       {
00131         std::cerr << __FILE__ << ":" << __LINE__ 
00132                   << " Cannot open file " << filename
00133                   << std::endl;
00134         return false;
00135       }
00136 
00137     std::ifstream fs(filename.c_str()) ;
00138 
00139     PHTimeStamp begin = getTimeStamp(fs);
00140     PHTimeStamp insert = getTimeStamp(fs);
00141 
00142     PHTimeStamp end;
00143     end.setToFarFuture();
00144 
00145     int index = 0;
00146     int nchannelPerFile = 144;
00147 
00148     while ( ( index < (nchannelPerFile - 1)) && !fs.eof() )
00149       {
00150         float value1;
00151         float value2;
00152         fs >> index >> value1 >> value2;
00153         tofFEM.AppendOneChannel(value1, value2);
00154       }
00155 
00156     fs.close();
00157     tofFEM.SetValidityPeriod(begin, end);
00158 
00159     return true;
00160   }
00161 
00162   emcOMAsciiT<emcLCTofFEM> 
00163   gemcOMLCTofFEM("emcOMLCTofFEM",
00164                  "Read/Write emcLCTofFEM objects from/to files",
00165                  reader,
00166                  writer);
00167 }