emcOMHLRatioFEM.C

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