emcOMRejectListT.h

Go to the documentation of this file.
00001 #ifndef __EMCOMREJECTLISTT_H__
00002 #define __EMCOMREJECTLISTT_H__
00003 
00004 #ifndef __EMCOBJECTMANAGER_H__
00005 #include "emcObjectManager.h"
00006 #endif
00007 
00008 class emcRejectList;
00009 class PdbCalBank;
00010 
00016 template <class BM>
00017 class emcOMRejectListT : public emcObjectManager
00018 {
00019 public:
00020 
00021   emcOMRejectListT(const char* name = "", const char* title = "");
00022 
00023   virtual ~emcOMRejectListT();
00024 
00025   bool CanRead(const emcManageable&) const;
00026 
00027   bool CanWrite(const emcManageable&) const;
00028 
00029   using emcObjectManager::Read;
00030 
00031   bool Read(emcManageable& object,
00032             const PHTimeStamp& time_stamp, int dummy);
00033 
00034   void Reset(void);
00035 
00036   bool Write(const emcManageable& object,
00037              const PHTimeStamp& tdummy,
00038              int dummy = -1);
00039 
00040 private:
00041   const char* GetPersistentClassName()
00042   {
00043     return "PdbEmcTowerRejectBank";
00044   }
00045   void FromPdbCalBank(emcRejectList&, PdbCalBank&);
00046   bool ReadFromFile(emcRejectList&);
00047   void ToPdbCalBank(const emcRejectList&, PdbCalBank&);
00048 
00049   BM* fBM;
00050 
00051   class changeName
00052   {
00053   public:
00054     changeName(const char* name)
00055     {
00056       name_ = emcManageable::GetStorageName(BM::storage());
00057       name_ += ":";
00058       name_ += name;
00059     }
00060 
00061     const char* c_str() const
00062           {
00063             return name_.c_str();
00064           }
00065 
00066       private:
00067         std::string name_;
00068       };
00069 
00070 };
00071 
00072 //_____________________________________________________________________________
00073 //_____________________________________________________________________________
00074 //_____________________________________________________________________________
00075 
00076 #include <cassert>
00077 #include <iostream>
00078 #include <sstream>
00079 #include <cstdio>
00080 #include "emcDataManager.h"
00081 #include "emcRejectList.h"
00082 #include "EmcIndexer.h"
00083 #include "PdbCalBank.hh"
00084 #include "PdbEmcTowerReject.hh"
00085 #include "emcDefines.h"
00086 #include "Pdb.hh"
00087 #include "PdbApplication.hh"
00088 #include "PdbBankManager.hh"
00089 #include <fstream>
00090 
00091 //_____________________________________________________________________________
00092 template<class BM>
00093 emcOMRejectListT<BM>::emcOMRejectListT(const char* name, const char* title)
00094   : emcObjectManager(changeName(name).c_str(),title), fBM(new BM)
00095 {
00096 }
00097 
00098 //_____________________________________________________________________________
00099 template<class BM>
00100 emcOMRejectListT<BM>::~emcOMRejectListT()
00101 {
00102   Reset();
00103 }
00104 
00105 //_____________________________________________________________________________
00106 template<class BM>
00107 bool
00108 emcOMRejectListT<BM>::CanRead(const emcManageable& object) const
00109 {
00110   if ( object.GetSource() != fBM->storage() )
00111     {
00112       return false;
00113     }
00114 
00115   const emcRejectList* rl = dynamic_cast<const emcRejectList*>(&object);
00116   
00117   if ( rl )
00118     {
00119       return true;
00120     }
00121   return false;
00122 }
00123 
00124 //_____________________________________________________________________________
00125 template<class BM>
00126 bool
00127 emcOMRejectListT<BM>::CanWrite(const emcManageable& object) const
00128 {
00129   if ( object.GetDestination() != fBM->storage() )
00130     {
00131       return false;
00132     }
00133 
00134   const emcRejectList* rl = dynamic_cast<const emcRejectList*>(&object);
00135   
00136   if ( rl )
00137     {
00138       return true;
00139     }
00140   return false;
00141 }
00142 
00143 
00144 //_____________________________________________________________________________
00145 template<class BM>
00146 void
00147 emcOMRejectListT<BM>::FromPdbCalBank(emcRejectList& rl, PdbCalBank& bank)
00148 {
00149   for ( size_t i = 0; i < bank.getLength(); ++i ) 
00150     {
00151       PdbEmcTowerReject* tr = static_cast<PdbEmcTowerReject*>
00152         (&(bank.getEntry(i)));
00153       assert(tr!=0);
00154       rl.set_or( tr->TowerId(), tr->AmplitudeError(), tr->AmplitudeWarning(),
00155                  tr->TimingError(), tr->TimingWarning());
00156     }
00157 }
00158 
00159 //_____________________________________________________________________________
00160 template<class BM>
00161 bool
00162 emcOMRejectListT<BM>::Read(emcManageable& object, 
00163                            const PHTimeStamp& time_stamp,int)
00164 {
00165   assert(CanRead(object));
00166   emcRejectList& rl = static_cast<emcRejectList&>(object);
00167 
00168   rl.Reset();
00169 
00170  if ( DM()->GetVerboseLevel() ) 
00171     {
00172       std::cout << EMC_INFO_MSG << " : Entering " << GetName()
00173              << "::Read (from Objy)" << std::endl;
00174     }
00175 
00176   PdbStatus status = fBM->DBApplication()->startRead() ;
00177 
00178   if (!status) 
00179     {
00180       std::cerr << EMC_ERROR_MSG << GetName() 
00181                 << "::Read : Could not open read "
00182                 << "transaction to Objy." << std::endl;
00183       fBM->DBApplication()->abort();
00184       return false;
00185     }
00186 
00187   PdbBankID bankID;
00188 
00189   bankID.setInternalValue(0);
00190 
00191   std::string pdb_classname = GetPersistentClassName();
00192   std::string basename = "calib.emc.";
00193   basename += rl.GetCategory();
00194 
00195   PdbCalBank* emcBank = 
00196     fBM->BankManager()->fetchBank(pdb_classname.c_str(),
00197                                   bankID,
00198                                   basename.c_str(),
00199                                   const_cast<PHTimeStamp&>(time_stamp));
00200   
00201   if (!emcBank) 
00202     {
00203       std::cerr << EMC_ERROR_MSG 
00204                 << GetName() << "::Read : Cannot fetch reject list bank "
00205                 << "at time " << time_stamp << std::endl;
00206       return false;
00207     }
00208   
00209   FromPdbCalBank(rl,*emcBank);
00210 
00211   delete emcBank;
00212 
00213   return fBM->DBApplication()->commit() ;
00214 }
00215 
00216 //_____________________________________________________________________________
00217 template<class BM>
00218 void
00219 emcOMRejectListT<BM>::Reset()
00220 {
00221 }
00222 
00223 //_____________________________________________________________________________
00224 template<class BM>
00225 void
00226 emcOMRejectListT<BM>::ToPdbCalBank(const emcRejectList& rl, PdbCalBank& bank)
00227 {
00228   size_t n = rl.size(); // this is the size of non-zeros 
00229 
00230   bank.setLength(n);
00231 
00232   size_t j = 0;
00233 
00234   for ( size_t i = 0; i < rl.maxsize(); ++i ) 
00235     {
00236       if (rl.nonZero(i))
00237         {
00238           PdbEmcTowerReject* tr = static_cast<PdbEmcTowerReject*>
00239             (&(bank.getEntry(j)));
00240           assert(tr!=0);
00241           tr->set(i,rl.AmplitudeError(i),rl.AmplitudeWarning(i),
00242                   rl.TimingError(i),rl.TimingWarning(i));
00243           ++j;
00244         }
00245     }
00246 
00247   assert(j==n);
00248 }
00249 
00250 //_____________________________________________________________________________
00251 template<class BM>
00252 bool 
00253 emcOMRejectListT<BM>::Write(const emcManageable& object, 
00254                             const PHTimeStamp&,int)
00255 {
00256   assert(CanWrite(object));
00257   const emcRejectList& rl = static_cast<const emcRejectList&>(object);
00258 
00259   bool written = false;
00260 
00261   if ( DM()->GetVerboseLevel() ) 
00262     {
00263       std::cout << EMC_INFO_MSG << " : Entering " 
00264                 << GetName() << "::Write " << std::endl;
00265     }
00266 
00267   PdbBankID bankID ;
00268 
00269   bankID.setInternalValue(0);
00270 
00271   std::string pdb_classname = GetPersistentClassName();
00272   std::string description = "Written by emcOMRejectList";
00273   std::string basename = "calib.emc.";
00274   basename += rl.GetCategory();
00275 
00276   PdbStatus status = fBM->DBApplication()->startUpdate();
00277 
00278   if ( !status ) 
00279     {
00280       std::cerr << EMC_ERROR_MSG << GetName() << "::Write : "
00281                 << "Cannot open a write transaction to Objy"
00282                 << std::endl;
00283       fBM->DBApplication()->abort();
00284       return false;
00285     }
00286 
00287   PHTimeStamp start = rl.GetStartValTime();
00288   PHTimeStamp end = rl.GetEndValTime();
00289 
00290   PdbCalBank* emcBank =  
00291     fBM->BankManager()->createBank(pdb_classname.c_str(),
00292                                    bankID,
00293                                    description.c_str(),
00294                                    start,
00295                                    end,
00296                                    basename.c_str()) ;
00297   if (!emcBank) 
00298     {
00299       return false;
00300     }
00301 
00302   ToPdbCalBank(rl,*emcBank);
00303 
00304   status = fBM->DBApplication()->commit();
00305 
00306   delete emcBank;
00307 
00308   if (status)
00309     {
00310       written=true;
00311     }
00312 
00313   return written;
00314 } 
00315 
00316 
00317 #endif