emcOMCalibrationDataTofSectorOffset.C

Go to the documentation of this file.
00001 #include "emcObjectManager.h"
00002 #include "emcManageable.h"
00003 #include <string>
00004 
00011 class emcOMCalibrationDataTofSectorOffset : public emcObjectManager
00012 {
00013 public:
00014   
00015   emcOMCalibrationDataTofSectorOffset(const char* name="", const char* title="");
00016   ~emcOMCalibrationDataTofSectorOffset();
00017 
00018   bool CanCollect(const emcManageable&) const
00019   { 
00020     return false;
00021   }
00022 
00026   bool CanRead(const emcManageable& object) const;
00027 
00031   bool CanWrite(const emcManageable& object) const;
00032 
00033   using emcObjectManager::Read;
00034 
00036   bool Read(emcManageable& object,
00037             const PHTimeStamp& time_stamp,
00038             int id);
00039   
00055   bool Read(emcManageable& object, int runnumber);
00056 
00057   void Reset() {}
00058 
00063   bool Write(const emcManageable&, const PHTimeStamp&,int);
00064   
00065 private:
00066   
00067   class changeName
00068   {
00069   public:
00070     changeName(const char* name)
00071     {
00072       name_ = emcManageable::GetStorageName(emcManageable::kFile_ASCII);
00073       name_ += ":";
00074       name_ += name;
00075     }
00076     
00077     const char* c_str() const
00078     {
00079       return name_.c_str();
00080     }
00081     
00082   private:
00083     std::string name_;
00084   };
00085 };
00086 
00087 #include "emcCalibrationData.h"
00088 #include "emcDataManager.h"
00089 #include "EmcIndexer.h"
00090 #include "asciitimestamp.h"
00091 #include "dirfilemanip.h"
00092 #include <fstream>
00093 #include <iostream>
00094 #include <cassert>
00095 #include <sstream>
00096 
00097 //_____________________________________________________________________________
00098 emcOMCalibrationDataTofSectorOffset::emcOMCalibrationDataTofSectorOffset(const char* name,
00099                                            const char* title)
00100   : emcObjectManager(changeName(name).c_str(),title)
00101 {
00102 }
00103 
00104 //_____________________________________________________________________________
00105 emcOMCalibrationDataTofSectorOffset::~emcOMCalibrationDataTofSectorOffset()
00106 {
00107 }
00108 
00109 //_____________________________________________________________________________
00110 bool
00111 emcOMCalibrationDataTofSectorOffset::CanRead(const emcManageable& object) const
00112 {
00113   if ( object.GetSource() != emcManageable::kFile_ASCII )
00114     {
00115       return false;
00116     }
00117   const emcCalibrationData* test = 
00118     dynamic_cast<const emcCalibrationData*>(&object);
00119   
00120   if ( test && test->GetType() == emcCalibrationData::kTofSectorOffset )
00121     {
00122       return true;
00123     }
00124   return false;
00125 }
00126 
00127 //_____________________________________________________________________________
00128 bool
00129 emcOMCalibrationDataTofSectorOffset::CanWrite(const emcManageable& object) const
00130 {
00131   if ( object.GetDestination() != emcManageable::kFile_ASCII )
00132     {
00133       return false;
00134     }
00135   const emcCalibrationData* test = 
00136     dynamic_cast<const emcCalibrationData*>(&object);
00137   
00138   if ( test && test->GetType() == emcCalibrationData::kTofSectorOffset )
00139     {
00140       return true;
00141     }
00142   return false;
00143 }
00144 
00145 //_____________________________________________________________________________
00146 bool
00147 emcOMCalibrationDataTofSectorOffset::Read(emcManageable& /*object*/,
00148                                           const PHTimeStamp& /*time_stamp*/,
00149                                           int /*id*/)
00150 {
00151   std::cerr << __FILE__ << ":" << __LINE__ << " is not implemented for this "
00152             << "object and data source="
00153             << emcManageable::GetStorageName(emcManageable::kFile_ASCII)
00154             << ". Please use Read(object,runnumber) instead"
00155             << std::endl;
00156   return false;
00157 }
00158 
00159 //_____________________________________________________________________________
00160 bool
00161 emcOMCalibrationDataTofSectorOffset::Read(emcManageable& object,
00162                                           int runnumber)
00163 { 
00164   emcCalibrationData& cal = static_cast<emcCalibrationData&>(object);
00165 
00166   cal.SetTypeAndSize(cal.GetType(),1);
00167 
00168   std::ostringstream str;
00169 
00170   emcDataManager* dm = emcDataManager::GetInstance();
00171 
00172   str << dm->GetSourceDir() << "/ToF/" << std::setfill('0')
00173       << std::setw(10) << runnumber << "/"
00174       << EmcIndexer::EmcSectorId(cal.GetNumber())
00175       << ".TOF_OFFSET";
00176 
00177   if ( dm->GetVerboseLevel() ) 
00178     {
00179       std::cout << "<I> " << __FILE__ << ":" << __LINE__ 
00180                 << " reading from "
00181                 << str.str() << std::endl;
00182     }
00183 
00184   std::ifstream in(str.str().c_str());
00185   if ( !in ) 
00186     {    
00187       std::cerr << "<E> Cannot open file "
00188            << str.str().c_str() << std::endl;
00189       std::cout << "Returning default object instead" << std::endl;
00190       cal.Set(0,0.0,0.0,0);
00191       cal.Set(0,0.0,0.0,1);
00192       cal.Set(0,0.0,0.0,2);
00193       cal.Set(0,0.0,0.0,3);
00194       cal.Set(0,0.0,0.0,4);
00195       return true;
00196     }
00197 
00198   int dim = 0;
00199   float a,b;
00200 
00201   while ( in >> a >> b )
00202     {
00203       cal.Set(0,a,b,dim);
00204       ++dim;
00205     }
00206 
00207   in.close();
00208 
00209   return true;
00210 }
00211 
00212 //_____________________________________________________________________________
00213 bool 
00214 emcOMCalibrationDataTofSectorOffset::Write(const emcManageable& object, 
00215                             const PHTimeStamp&,int)
00216 {
00217   const emcCalibrationData& cal = 
00218     static_cast<const emcCalibrationData&>(object);
00219 
00220   // sanity checks, again
00221   if ( !cal.GetType() == emcCalibrationData::kTofSectorOffset )
00222     {
00223       std::cerr << __FILE__ << ":" << __LINE__  << " cal is of wrong "
00224                 << "type!!! This should not happen as it should be "
00225                 << "checked by the CanRead/CanWrite methods" << std::endl;
00226       return false;
00227     }
00228   if ( !cal.GetSize() == 1 )
00229     {
00230       std::cerr << __FILE__ << ":" << __LINE__  << " cal is of wrong "
00231                 << " size. Should be 1." 
00232                 << std::endl;
00233       return false;
00234     }
00235 
00236   std::ostringstream dirname;
00237 
00238   emcDataManager* dm = emcDataManager::GetInstance();
00239 
00240   const int dim_run = 0;
00241 
00242   dirname << dm->GetDestinationDir() << "/ToF/" 
00243           << std::setfill('0') << std::setw(10) << cal.GetValue(0,dim_run);
00244 
00245   if (!createDirectory(dirname.str()))
00246     {
00247       return false;
00248     }
00249   
00250   std::ostringstream filename;
00251 
00252   int isector = cal.GetNumber();
00253 
00254   filename << expand(dirname.str()) << "/" << EmcIndexer::EmcSectorId(isector)
00255            << ".TOF_OFFSET";
00256 
00257   std::ofstream out(filename.str().c_str());
00258 
00259   if (!out)
00260     {
00261       return false;
00262     }
00263 
00264   for ( size_t i = 0; i < 5; ++i )
00265     {
00266       out << cal.GetValue(0,i) << " " << cal.GetError(0,i)
00267           << std::endl;
00268     }
00269 
00270   out.close();
00271 
00272   return true;      
00273 }
00274 
00275 emcOMCalibrationDataTofSectorOffset gemcOMCalibrationDataTofSectorOffset("emcOMCalibrationDataTofSectorOffset","Handle Run-by-Run Sector T0 offsets");