00001 #include "emcObjectManager.h"
00002 #include "emcManageable.h"
00003 #include "phool.h"
00004
00009 class emcOMRejectList : public emcObjectManager
00010 {
00011 public:
00012 emcOMRejectList(const char* name, const char* title);
00013 virtual ~emcOMRejectList();
00014
00015 bool CanRead(const emcManageable&) const;
00016 bool CanWrite(const emcManageable&) const;
00017
00018 using emcObjectManager::Read;
00019
00023 bool Read(emcManageable& object,
00024 const PHTimeStamp& time_stamp,
00025 int id);
00026
00027 void Reset() {}
00028
00029 bool Write(const emcManageable& object,
00030 const PHTimeStamp& tdummy,
00031 int dummy = -1);
00032
00033 private:
00034 class changeName
00035 {
00036 public:
00037 changeName(const char* name)
00038 {
00039 name_ = emcManageable::GetStorageName(emcManageable::kFile_ASCII);
00040 name_ += ":";
00041 name_ += name;
00042 }
00043
00044 const char* c_str() const
00045 {
00046 return name_.c_str();
00047 }
00048
00049 private:
00050 std::string name_;
00051 };
00052 };
00053
00054 #include "emcRejectList.h"
00055 #include "emcDefines.h"
00056 #include <iostream>
00057 #include "EmcIndexer.h"
00058 #include "emcDataManager.h"
00059 #include <fstream>
00060 #include <sstream>
00061 #include <cstdio>
00062 #include "TSystem.h"
00063
00064
00065 emcOMRejectList::emcOMRejectList(const char* name, const char* title)
00066 : emcObjectManager(changeName(name).c_str(),title)
00067 {
00068 }
00069
00070
00071 emcOMRejectList::~emcOMRejectList()
00072 {
00073 }
00074
00075
00076 bool
00077 emcOMRejectList::CanRead(const emcManageable& object) const
00078 {
00079 if ( object.GetSource() != emcManageable::kFile_ASCII )
00080 {
00081 return false;
00082 }
00083 const emcRejectList* test = dynamic_cast<const emcRejectList*>(&object);
00084
00085 if (test)
00086 {
00087 return true;
00088 }
00089 else
00090 {
00091 return false;
00092 }
00093 }
00094
00095
00096 bool
00097 emcOMRejectList::CanWrite(const emcManageable& object) const
00098 {
00099 if ( object.GetDestination() != emcManageable::kFile_ASCII )
00100 {
00101 return false;
00102 }
00103 const emcRejectList* test = dynamic_cast<const emcRejectList*>(&object);
00104
00105 if (test)
00106 {
00107 return true;
00108 }
00109 else
00110 {
00111 return false;
00112 }
00113 }
00114
00115
00116 bool
00117 emcOMRejectList::Read(emcManageable& object,
00118 const PHTimeStamp& ,
00119 int )
00120 {
00121 emcRejectList& rl = static_cast<emcRejectList&>(object);
00122
00123 if ( DM()->GetVerboseLevel() )
00124 {
00125 std::cout << EMC_INFO_MSG << " : Entering " << GetName()
00126 << "::Read" << std::endl;
00127 }
00128
00129 emcDataManager* dm = emcDataManager::GetInstance();
00130
00131 std::ostringstream str;
00132 char* stmpdir = gSystem->ExpandPathName(dm->GetSourceDir());
00133 str << stmpdir << "/RejectList/reject.list";
00134 delete[] stmpdir;
00135
00136 std::ifstream in(str.str().c_str());
00137 if ( !in )
00138 {
00139 std::cerr << "<E> Cannot open file " << str.str().c_str() << std::endl;
00140 return false;
00141 }
00142
00143 char line[255];
00144
00145 int sector,z,y;
00146 int amplError,tofError,amplWarning,tofWarning;
00147
00148 while (in.getline(line, 255, '\n'))
00149 {
00150
00151 if (line[0]=='#')
00152 {
00153 continue;
00154 }
00155
00156 if ( 7 != sscanf(line, "%d %d %d %d %d %d %d", §or, &z, &y,
00157 &lError, &tofError, &lWarning, &tofWarning))
00158 {
00159 continue;
00160 }
00161
00162 if ( DM()->GetVerboseLevel()>=10 )
00163 {
00164 printf("%d %d %d %d %d %d %d\n", sector, z, y,
00165 amplError, tofError, amplWarning, tofWarning);
00166 }
00167
00168 if (EmcIndexer::IsValid(sector, z, y))
00169 {
00170 int towerid = EmcIndexer::getTowerId(sector, z, y);
00171 rl.set_or(towerid,amplError,amplWarning,tofError,tofWarning);
00172 }
00173 else
00174 {
00175 std::cerr << "<E> emcOMRejectList::ReadFromFile : no such tower : "
00176 << "sector=" << sector << " z=" << z << " y=" << y
00177 << std::endl;
00178 }
00179 }
00180 in.close();
00181
00182 return true;
00183 }
00184
00185 bool
00186 emcOMRejectList::Write(const emcManageable& object,
00187 const PHTimeStamp&,
00188 int)
00189 {
00190 const emcRejectList& rl = static_cast<const emcRejectList&>(object);
00191
00192 if ( DM()->GetVerboseLevel() )
00193 {
00194 std::cout << EMC_INFO_MSG << " : Entering " << GetName()
00195 << "::Write" << std::endl;
00196 }
00197
00198 emcDataManager* dm = emcDataManager::GetInstance();
00199
00200 std::ostringstream str;
00201 char* stmpdir = gSystem->ExpandPathName(dm->GetDestinationDir());
00202 str << stmpdir << "/RejectList";
00203 delete[] stmpdir;
00204
00205 void* pdir = gSystem->OpenDirectory(str.str().c_str());
00206 if (!pdir)
00207 {
00208 if ( DM()->GetVerboseLevel() )
00209 {
00210 std::cout << PHWHERE << " Trying to create dir "
00211 << str.str() << std::endl;
00212 }
00213 int bad = gSystem->mkdir(str.str().c_str(),true);
00214 if (bad)
00215 {
00216 std::cerr << PHWHERE << " Could not create directory "
00217 << str.str()
00218 << std::endl;
00219 gSystem->FreeDirectory(pdir);
00220 return false;
00221 }
00222 }
00223
00224 gSystem->FreeDirectory(pdir);
00225
00226 str << "/reject.list";
00227
00228 std::ofstream out(str.str().c_str());
00229 if ( !out )
00230 {
00231 std::cerr << "<E> Cannot create file " << str.str().c_str() << std::endl;
00232 return false;
00233 }
00234
00235 out << "# Written by DataManager plug-in=[" << GetName()
00236 << "] on " << PHTimeStamp() << std::endl;
00237
00238 for ( size_t towerid = 0; towerid < 144*172; ++towerid )
00239 {
00240 if ( rl.nonZero(towerid) )
00241 {
00242
00243
00244 int sector,z,y;
00245 EmcIndexer::decodeTowerId(towerid,sector,z,y);
00246 out << sector << " " << z << " " << y << " "
00247 << rl.AmplitudeError(towerid) << " "
00248 << rl.TimingError(towerid) << " "
00249 << rl.AmplitudeWarning(towerid) << " "
00250 << rl.TimingWarning(towerid)
00251 << std::endl;
00252 }
00253 }
00254
00255 out.close();
00256 return true;
00257 }
00258
00259 namespace
00260 {
00261 emcOMRejectList gemcOMRejectList("emcOMRejectList","Read EMCAL reject list");
00262 }