00001 #include "emcOMAsciiT.h"
00002 #include "emcTracedFEM.h"
00003 #include "emcDataManager.h"
00004 #include "EmcIndexer.h"
00005 #include "emcTracedValue.h"
00006 #include "emcTofT0FEM.h"
00007 #include "asciitimestamp.h"
00008 #include "dirfilemanip.h"
00009 #include "TSystem.h"
00010 #include <string>
00011 #include <vector>
00012 #include <fstream>
00013 #include <sstream>
00014 #include <iostream>
00015 #include <iomanip>
00016
00017 using namespace std;
00018
00019 namespace
00020 {
00021 bool ReadOneFEMFromFile(std::string filename, emcTracedFEM& fem,
00022 int)
00023 {
00024
00025
00026 emcDataManager* dm = emcDataManager::GetInstance();
00027
00028 std::ifstream fin(filename.c_str());
00029
00030 if ( !fin )
00031 {
00032 std::cerr << __FILE__ << ":" << __LINE__
00033 << " Cannot open "
00034 << filename << std::endl;
00035 return false;
00036 }
00037
00038 if ( dm->GetVerboseLevel() > 0 )
00039 {
00040 std::cout << __FILE__ << ":" << __LINE__
00041 << " Reading file "
00042 << filename << std::endl;
00043 }
00044
00045 fem.SetNumberOfChannels(144);
00046
00047 PHTimeStamp tStart = getTimeStamp(fin);
00048
00049 int i;
00050 int thex;
00051 float theconstant;
00052 float theslope;
00053
00054 bool end = false;
00055 bool oldversion = true;
00056
00057 while ( fin >> i >> thex >> theconstant >> theslope && !end)
00058 {
00059
00060 if (i < 144)
00061 {
00062 fem.AddNewItem(i, new emcTracedValue(thex, theconstant, theslope));
00063 }
00064 else
00065 {
00066
00067 if ( i == 144 )
00068 {
00069
00070 assert (theconstant == 0);
00071 assert (theslope == 0);
00072 fem.SetXmax(thex);
00073 }
00074 fin >> i >> thex >> theconstant >> theslope;
00075 if ( i == 145 )
00076 {
00077 assert (theconstant == 0);
00078 assert (theslope == 0);
00079 fem.SetXmin(thex);
00080 oldversion = false;
00081 }
00082 if (!fin.eof())
00083 {
00084 fin >> i >> thex >> theconstant >> theslope;
00085 if ( i == 146 )
00086 {
00087 assert(thex==0);
00088 assert(theslope==0);
00089 assert(strcmp(fem.GetCategory(),"TofT0Bs")==0);
00090 (static_cast<emcTofT0FEM&>(fem)).setBBCT0(theconstant);
00091 }
00092 }
00093 end = true;
00094 }
00095 }
00096
00097 fin.close();
00098
00099 PHTimeStamp tEnd;
00100 tEnd.setToFarFuture();
00101
00102 fem.SetValidityPeriod(tStart, tEnd);
00103
00104 if ( oldversion == true && EmcIndexer::isPbScFEM(fem.AbsolutePosition()) )
00105 {
00106
00107 phtime_t tics0 = tStart.getTics();
00108 fem.SetXmin( tics0 );
00109 fem.SetXmax( fem.GetXmax() + tics0 );
00110 }
00111
00112 return true;
00113 }
00114
00115 bool namer(const emcTracedFEM& tracedFEM,
00116 int to_or_from,
00117 std::string& filename)
00118 {
00119 int SN, SM144N;
00120
00121 EmcIndexer::PXSM144_iSiSM144(tracedFEM.AbsolutePosition(), SN, SM144N);
00122
00123 emcDataManager* dm = emcDataManager::GetInstance();
00124
00125 std::string dirname;
00126
00127 if ( to_or_from == 0 )
00128 {
00129 dirname = dm->GetSourceDir();
00130 }
00131 else
00132 {
00133 dirname = dm->GetDestinationDir();
00134 }
00135
00136 std::string category = tracedFEM.GetCategory();
00137 std::string FileExt;
00138 std::string wTracerName;
00139
00140 if ( category == "Gains" || category == "GainsV1" )
00141 {
00142
00143 dirname += "/Gains/";
00144 FileExt = "GAINS";
00145
00146 if ( SN < 6 )
00147 {
00148 wTracerName = "LASER-TOWERS-NORM_ADC";
00149 }
00150 else
00151 {
00152 wTracerName = "LED(AVY)-TOWERS-NORM_ADC";
00153 }
00154 }
00155 else if ( category == "TofT0s" || category == "TofT0Bs" )
00156 {
00157 dirname += "/ToF/";
00158 FileExt = "TofT0s";
00159
00160 if ( SN < 6 )
00161 {
00162 wTracerName = "LASER-TOWERS-T0-DRIFT";
00163 }
00164 else
00165 {
00166 wTracerName = "LED-TOWERS-T0-DRIFT";
00167 }
00168 }
00169 else if ( category == "TacPeds")
00170 {
00171 dirname += "/ToF/";
00172 FileExt = "TofT0s";
00173 wTracerName = "PED-TOWERS-TAC-DRIFT";
00174 }
00175 else
00176 {
00177 std::cerr << __FILE__ << ":" << __LINE__
00178 << " Don't know how to handle category "
00179 << category << std::endl;
00180 return false;
00181 }
00182
00183 if ( to_or_from )
00184 {
00185 if (!createDirectory(dirname))
00186 {
00187 return false;
00188 }
00189 }
00190
00191 std::ostringstream sfilename;
00192
00193 sfilename << expand(dirname) << "/"
00194 << EmcIndexer::EmcSectorId(SN)
00195 << "SM"
00196 << SM144N
00197 << "."
00198 << wTracerName
00199 << "."
00200 << FileExt;
00201
00202 filename = sfilename.str();
00203
00204 return true;
00205 }
00206
00207 bool reader(emcTracedFEM& tracedFEM, int code)
00208 {
00209 if ( code >= 0 )
00210 {
00211 int femAbsPosition;
00212 int idummy;
00213 emcCalFEM::FEMDecode(code,femAbsPosition,idummy,idummy,idummy);
00214 if (femAbsPosition!=tracedFEM.AbsolutePosition())
00215 {
00216 std::cerr << __FILE__ << ":" << __LINE__
00217 << " code mismatch with absolutePosition"
00218 << std::endl;
00219 return false;
00220 }
00221 }
00222
00223 std::string filename;
00224
00225 bool ok = namer(tracedFEM,0,filename);
00226
00227 if (!ok)
00228 {
00229 return false;
00230 }
00231
00232 if (!checkFile(filename))
00233 {
00234 std::cerr << __FILE__ << ":" << __LINE__
00235 << " Cannot open file " << filename
00236 << std::endl;
00237 return false;
00238 }
00239
00240 return ReadOneFEMFromFile(filename, tracedFEM,
00241 tracedFEM.AbsolutePosition());
00242 }
00243
00244 bool writer(const emcTracedFEM& tracedFEM, int)
00245 {
00246 std::string filename;
00247 bool ok = namer(tracedFEM,1,filename);
00248 if (!ok)
00249 {
00250 return false;
00251 }
00252
00253 if (checkFile(filename))
00254 {
00255 std::cerr << __FILE__ << ":" << __LINE__
00256 << " File " << filename
00257 << " is on the way. Remove it first"
00258 << std::endl;
00259 return false;
00260 }
00261
00262 std::ofstream out(filename.c_str());
00263 if (!out.good())
00264 {
00265 std::cerr << __FILE__ << ":" << __LINE__
00266 << " Could not create file " << filename
00267 << std::endl;
00268 return false;
00269 }
00270
00271 out << tracedFEM.GetStartValTime() << std::endl;
00272
00273 for ( size_t i = 0; i < tracedFEM.size(); ++i )
00274 {
00275 tracedFEM.FirstItem(i);
00276 emcTracedValue* tv;
00277 while ( ( tv = tracedFEM.NextItem() ) )
00278 {
00279 std::ostream::fmtflags oldflags = out.flags();
00280 out << std::setw(6) << i << " ";
00281
00282
00283
00284
00285 out.setf(std::istream::scientific);
00286 out << std::setw(14) << tv->GetX() << " ";
00287 out << std::setw(14) << tv->GetConstant() << " ";
00288 out << std::setw(14) << tv->GetSlope()
00289 << std::endl;
00290 out.setf(oldflags);
00291 }
00292 }
00293
00294
00295 out << "144 " << tracedFEM.GetXmax() << " 0 0" << std::endl;
00296 out << "145 " << tracedFEM.GetXmin() << " 0 0" << std::endl;
00297
00298 if ( strcmp(tracedFEM.GetCategory(),"TofT0Bs")==0 &&
00299 tracedFEM.Version()>0 )
00300 {
00301 out << "146 0 "
00302 << static_cast<const emcTofT0FEM&>(tracedFEM).getBBCT0()
00303 << " 0" << std::endl;
00304 }
00305
00306 out.close();
00307
00308 return true;
00309 }
00310
00311 emcOMAsciiT<emcTracedFEM> gemcOMTracedFEM("emcOMTracedFEM",
00312 "Read/Write emcTracedFEM objects",
00313 reader,
00314 writer);
00315 }