emcOMCalibrationDataIniCal.C

Go to the documentation of this file.
00001 #include "emcObjectManager.h"
00002 #include "emcManageable.h"
00003 #include <string>
00004 
00010 class emcOMCalibrationDataIniCal : public emcObjectManager
00011 {
00012 public:
00013   
00014   emcOMCalibrationDataIniCal(const char* name="", const char* title="");
00015   ~emcOMCalibrationDataIniCal();
00016 
00017   bool CanCollect(const emcManageable&) const
00018   { 
00019     return false;
00020   }
00021 
00023   bool CanRead(const emcManageable& object) const;
00024 
00026   bool CanWrite(const emcManageable& object) const;
00027 
00028   using emcObjectManager::Read;
00029 
00046   bool Read(emcManageable& object,
00047             const PHTimeStamp& time_stamp,
00048             int id);
00049   
00050   void Reset() {}
00051 
00056   bool Write(const emcManageable&, const PHTimeStamp&,int);
00057   
00058 private:
00059   
00060   class changeName
00061   {
00062   public:
00063     changeName(const char* name)
00064     {
00065       name_ = emcManageable::GetStorageName(emcManageable::kFile_ASCII);
00066       name_ += ":";
00067       name_ += name;
00068     }
00069     
00070     const char* c_str() const
00071     {
00072       return name_.c_str();
00073     }
00074     
00075   private:
00076     std::string name_;
00077   };
00078 };
00079 
00080 #include "emcCalibrationData.h"
00081 #include "emcDataManager.h"
00082 #include "EmcIndexer.h"
00083 #include "dirfilemanip.h"
00084 #include <fstream>
00085 #include <iostream>
00086 #include <cassert>
00087 #include <sstream>
00088 
00089 //_____________________________________________________________________________
00090 emcOMCalibrationDataIniCal::emcOMCalibrationDataIniCal(const char* name,
00091                                            const char* title)
00092   : emcObjectManager(changeName(name).c_str(),title)
00093 {
00094 }
00095 
00096 //_____________________________________________________________________________
00097 emcOMCalibrationDataIniCal::~emcOMCalibrationDataIniCal()
00098 {
00099 }
00100 
00101 //_____________________________________________________________________________
00102 bool
00103 emcOMCalibrationDataIniCal::CanRead(const emcManageable& object) const
00104 {
00105   if ( object.GetSource() != emcManageable::kFile_ASCII )
00106     {
00107       return false;
00108     }
00109   const emcCalibrationData* test = 
00110     dynamic_cast<const emcCalibrationData*>(&object);
00111   
00112   if ( test && test->GetType() == emcCalibrationData::kIniCal )
00113     {
00114       return true;
00115     }
00116   return false;
00117 }
00118 
00119 //_____________________________________________________________________________
00120 bool
00121 emcOMCalibrationDataIniCal::CanWrite(const emcManageable& object) const
00122 {
00123   if ( object.GetDestination() != emcManageable::kFile_ASCII )
00124     {
00125       return false;
00126     }
00127   const emcCalibrationData* test = 
00128     dynamic_cast<const emcCalibrationData*>(&object);
00129   
00130   if ( test && test->GetType() == emcCalibrationData::kIniCal )
00131     {
00132       return true;
00133     }
00134   return false;
00135 }
00136 
00137 //_____________________________________________________________________________
00138 bool
00139 emcOMCalibrationDataIniCal::Read(emcManageable& object,
00140                            const PHTimeStamp& /*time_stamp*/,
00141                            int /*id*/)
00142 { 
00143   emcCalibrationData& cal = static_cast<emcCalibrationData&>(object);
00144 
00145   size_t length = 2592; // # of PbSc towers per PbSc sector
00146   if ( cal.GetNumber() > 5 ) 
00147     {
00148       length = 4608; // # of PbGl towers per PbGl sector
00149     }
00150 
00151   cal.SetTypeAndSize(cal.GetType(),length);
00152 
00153   std::ostringstream str;
00154 
00155   emcDataManager* dm = emcDataManager::GetInstance();
00156 
00157   str << dm->GetSourceDir() << "/IniCal/" 
00158       << EmcIndexer::EmcSectorId(cal.GetNumber())
00159       << ".INICAL";
00160 
00161   if ( dm->GetVerboseLevel() ) 
00162     {
00163       std::cout << "<I> " << __FILE__ << ":" << __LINE__ 
00164                 << " reading from "
00165                 << str.str() << std::endl;
00166     }
00167 
00168   std::ifstream in(str.str().c_str());
00169   if ( !in ) 
00170     {    
00171       std::cerr << "<E> Cannot open file "
00172            << str.str().c_str() << std::endl;
00173       return false;
00174     }
00175   int fem, femchannel;
00176   float c0,g0,one;
00177 
00178   int sectorNumber = cal.GetNumber();
00179 
00180   while ( in >> fem >> femchannel >> c0 >> g0 >> one )
00181     {
00182       int towerid = EmcIndexer::PXSM144iCH_iPX(fem,femchannel);
00183       int ist;
00184       EmcIndexer::iPXiSiST(towerid,sectorNumber,ist);
00185       cal.Set(ist,c0,0,0);
00186       cal.Set(ist,g0,0,1);
00187       cal.Set(ist,one,0,2);
00188     }
00189 
00190   return true;
00191 }
00192 
00193 //_____________________________________________________________________________
00194 bool 
00195 emcOMCalibrationDataIniCal::Write(const emcManageable& object, 
00196                             const PHTimeStamp&,int)
00197 {
00198   const emcCalibrationData& cal = 
00199     static_cast<const emcCalibrationData&>(object);
00200   
00201   std::ostringstream dirname;
00202 
00203   emcDataManager* dm = emcDataManager::GetInstance();
00204 
00205   dirname << dm->GetDestinationDir() << "/IniCal";
00206 
00207   if (!createDirectory(dirname.str()))
00208     {
00209       return false;
00210     }
00211   
00212   std::ostringstream filename;
00213 
00214   int isector = cal.GetNumber();
00215 
00216   filename << expand(dirname.str()) << "/"
00217            << EmcIndexer::EmcSectorId(isector)
00218            << ".INICAL";
00219 
00220   std::ofstream out(filename.str().c_str());
00221 
00222   if (!out)
00223     {
00224       return false;
00225     }
00226  
00227   for ( size_t i = 0; i < cal.GetSize(); ++i )
00228     {
00229       // i = ist = sector tower number, i.e. within the sector.
00230       int towerid = EmcIndexer::iSiSTiPX(isector,i);
00231       int fem, femchannel;
00232       EmcIndexer::PXPXSM144CH(towerid,fem,femchannel);
00233       out << fem << " " << femchannel << " ";
00234       for ( size_t j = 0; j < cal.GetDimension(); ++j )
00235         {
00236           out << cal.GetValue(i,j) << " ";
00237         }
00238       out << std::endl;
00239     }
00240 
00241   out.close();
00242 
00243   return true;      
00244 }
00245 
00246 emcOMCalibrationDataIniCal gemcOMCalibrationDataIniCal("emcOMCalibrationDataIniCal","Handle IniCal (absolute EMCAL calibration)");