emcOMWalkTofFEM.C

Go to the documentation of this file.
00001 #include "emcOMAsciiT.h"
00002 #include "emcWalkTofFEM.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 emcWalkTofFEM& 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_WALK";
00050 
00051     filename = sfilename.str();
00052 
00053     return true;
00054   }
00055 
00056   bool writer(const emcWalkTofFEM& 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 << " " 
00088             << tofFEM.getValue(i,0) << " "
00089             << tofFEM.getValue(i,1) << " "
00090             << 0 << std::endl;
00091       }
00092 
00093     out.close();
00094 
00095     emcDataManager* dm = emcDataManager::GetInstance();
00096     if ( dm->GetVerboseLevel() )
00097       {
00098         std::cout << "emcOMWalkTofFEM written to " 
00099                   << filename << std::endl;
00100       }
00101 
00102     return true;
00103   }
00104 
00105   bool reader(emcWalkTofFEM& tofFEM, int code)
00106   {
00107     if ( code >= 0 )
00108       {
00109         int femAbsPosition;
00110         int idummy;
00111         emcCalFEM::FEMDecode(code,femAbsPosition,idummy,idummy,idummy);
00112         if (femAbsPosition!=tofFEM.AbsolutePosition())
00113           {
00114             std::cerr << __FILE__ << ":" << __LINE__
00115                       << " code mismatch with absolutePosition"
00116                       << std::endl;
00117             return false;
00118           }
00119       }
00120 
00121     std::string filename;
00122 
00123     bool ok = namer(tofFEM,0,filename);
00124 
00125     if (!ok)
00126       {
00127         return false;
00128       }
00129 
00130     if (!checkFile(filename))
00131       {
00132         std::cerr << __FILE__ << ":" << __LINE__ 
00133                   << " Cannot open file " << filename
00134                   << std::endl;
00135         return false;
00136       }
00137 
00138     std::ifstream fs(filename.c_str());
00139 
00140     PHTimeStamp begin = getTimeStamp(fs);
00141     PHTimeStamp insert = getTimeStamp(fs);
00142 
00143     PHTimeStamp end;
00144     end.setToFarFuture();
00145 
00146     int index = 0;
00147     int nchannelPerFile = 144;
00148 
00149     while ( ( index < (nchannelPerFile - 1)) && !fs.eof() )
00150       {
00151         float value1;
00152         float value2;
00153         float dummy;
00154         fs >> index >> value1 >> value2 >> dummy;
00155         tofFEM.AppendOneChannel(value1, value2);
00156       }
00157 
00158     fs.close();
00159     tofFEM.SetValidityPeriod(begin, end);
00160 
00161     return true;
00162   }
00163 
00164   emcOMAsciiT<emcWalkTofFEM> 
00165   gemcOMWalkTofFEM("emcOMWalkTofFEM",
00166                    "Read/Write emcWalkTofFEM objects from/to files",
00167                    reader,
00168                    writer);
00169 }