emcOMGeometryT.h

Go to the documentation of this file.
00001 #ifndef __EMCOMGEOMETRYT_H__
00002 #define __EMCOMGEOMETRYT_H__
00003 
00004 #ifndef __EMCMANAGEABLE_H__
00005 #include "emcManageable.h"
00006 #endif
00007 #ifndef __EMCOBJECTMANAGER_H__
00008 #include "emcObjectManager.h"
00009 #endif
00010 
00011 #include <string>
00012 #include <iostream>
00013 
00014 class emcGeometry;
00015 
00021 template <class BM>
00022 class emcOMGeometryT : public emcObjectManager
00023 {
00024 
00025 public:
00026 
00027   emcOMGeometryT(const char* name = "", const char* title = "");
00028   virtual ~emcOMGeometryT();
00029 
00031   virtual bool CanCollect(const emcManageable& object) const;
00032 
00034   virtual bool CanWrite(const emcManageable& object) const;
00035 
00037   virtual bool CanRead(const emcManageable& object) const;
00038 
00045   virtual emcManageable* Collect(const emcManageable& object,
00046                                  const PHTimeStamp& when);
00047 
00048 
00049   using emcObjectManager::Read;
00050 
00054   virtual bool Read(emcManageable& object,
00055                     const PHTimeStamp& time_stamp,
00056                     int id);
00057 
00061   virtual void Reset(void);
00062 
00066   virtual bool Write(const emcManageable& object,
00067                      const PHTimeStamp& tStart,
00068                      int id = -1);
00069 
00070 private:
00071   emcGeometry* fGeometry;
00072   BM* fBM;
00073 
00074 private:
00075   class changeName
00076   {
00077   public:
00078     changeName(const char* name)
00079     {
00080       name_ = emcManageable::GetStorageName(BM::storage());
00081       name_ += ":";
00082       name_ += name;
00083     }
00084 
00085     const char* c_str() const
00086     {
00087       return name_.c_str();
00088     }
00089 
00090   private:
00091     std::string name_;
00092   };
00093 };
00094 
00095 //_____________________________________________________________________________
00096 //_____________________________________________________________________________
00097 //_____________________________________________________________________________
00098 
00099 #include <iostream>
00100 #include <vector>
00101 
00102 #ifndef __EMCDATAMANAGER_H__
00103 #include "emcDataManager.h"
00104 #endif
00105 #ifndef __EMCGEOMETRY_H__
00106 #include "emcGeometry.h"
00107 #endif
00108 #include "PdbCalBank.hh"
00109 #include "PdbBankList.hh"
00110 #include "PdbBankListIterator.hh"
00111 #include "PdbEmcSecGeom.hh"
00112 #include "Pdb.hh"
00113 #include "PdbApplication.hh"
00114 #include "PdbBankManager.hh"
00115 
00116 //_____________________________________________________________________________
00117 template <class BM>
00118 emcOMGeometryT<BM>::emcOMGeometryT(const char* name, const char* title) :
00119   emcObjectManager(changeName(name).c_str(), title),
00120   fGeometry(0),
00121   fBM(new BM)
00122 {
00123 }
00124 
00125 //_____________________________________________________________________________
00126 template <class BM>
00127 emcOMGeometryT<BM>::~emcOMGeometryT()
00128 {
00129   Reset();
00130 }
00131 
00132 
00133 //_____________________________________________________________________________
00134 template <class BM>
00135 bool
00136 emcOMGeometryT<BM>::CanCollect(const emcManageable& object) const
00137 {
00138   return CanRead(object);
00139 }
00140 
00141 
00142 //_____________________________________________________________________________
00143 template <class BM>
00144 bool
00145 emcOMGeometryT<BM>::CanWrite(const emcManageable& object) const
00146 {
00147   if (object.GetDestination() != fBM->storage())
00148     {
00149       return false;
00150     }
00151 
00152   const emcManageable* object_ptr = &object;
00153 
00154   const emcGeometry* test =
00155     dynamic_cast<const emcGeometry*>(object_ptr);
00156 
00157   if ( test )
00158     {
00159       return true;
00160     }
00161 
00162   return false;
00163 }
00164 
00165 //_____________________________________________________________________________
00166 template <class BM>
00167 bool
00168 emcOMGeometryT<BM>::CanRead(const emcManageable& object) const
00169 {
00170   if ( object.GetSource() != fBM->storage() )
00171     {
00172       return false;
00173     }
00174 
00175   const emcManageable* object_ptr = &object;
00176 
00177   const emcGeometry* test =
00178     dynamic_cast<const emcGeometry*>(object_ptr);
00179 
00180   if ( test )
00181     {
00182       return true;
00183     }
00184 
00185   return false;
00186 }
00187 
00188 //_____________________________________________________________________________
00189 template <class BM>
00190 emcManageable*
00191 emcOMGeometryT<BM>::Collect(const emcManageable&,
00192                             const PHTimeStamp& when)
00193 {
00194   if ( fGeometry )
00195     {
00196       if (fGeometry->IsValid(when))
00197         {
00198           return fGeometry;
00199         }
00200     }
00201 
00202   if ( !fGeometry )
00203     {
00204       fGeometry = new emcGeometry();
00205     }
00206 
00207   bool ok = Read(*fGeometry, when, 1);
00208 
00209   if (!ok)
00210     {
00211       delete fGeometry;
00212       fGeometry = 0;
00213     }
00214 
00215   return fGeometry;
00216 
00217 }
00218 
00219 //_____________________________________________________________________________
00220 template<class BM>
00221 bool
00222 emcOMGeometryT<BM>::Read(emcManageable& object,
00223                          const PHTimeStamp& time_stamp,
00224                          int id)
00225 {
00226   bool kIsRead = false;
00227 
00228   emcGeometry& geometry = static_cast<emcGeometry&>(object);
00229   geometry.Reset();
00230 
00231   PdbStatus status = fBM->DBApplication()->startRead();
00232 
00233   if (!status)
00234     {
00235       std::cerr << "<E> emcOMGeometryT<" << BM::name()
00236                 << ">::Read : cannot open a read transaction"
00237            << std::endl;
00238       fBM->DBApplication()->abort();
00239       return false;
00240     }
00241 
00242   if (id<0) id=1;
00243 
00244   PdbBankID bankID(id);
00245   std::string pdb_classname = "PdbEmcSecGeomBank";
00246   std::string basename = geometry.GetCategory();
00247 
00248   PdbCalBank* emcBank =
00249     fBM->BankManager()->fetchBank(pdb_classname.c_str(),
00250                                   bankID,
00251                                   basename.c_str(),
00252                                   const_cast<PHTimeStamp&>(time_stamp));
00253 
00254   if (!emcBank)
00255     {
00256       std::cerr << "<E> emcOMGeometryT<" << BM::name()
00257            << ">::Read : cannot fetch geometry for time = "
00258            << time_stamp << std::endl;
00259       fBM->DBApplication()->abort();
00260       return false;
00261     }
00262 
00263 
00264   kIsRead = true;
00265 
00266   size_t nsectors = emcBank->getLength();
00267   assert (nsectors > 0 && nsectors <= emcGeometry::MaxNumberOfSectors());
00268   PdbEmcSecGeom* pdbsec;
00269 
00270   std::vector<SecGeom> theSectors(nsectors);
00271 
00272   for ( size_t iS = 0; iS < nsectors; iS++ )
00273     {
00274 
00275       SecGeom& sec = theSectors[iS];
00276 
00277       pdbsec = static_cast<PdbEmcSecGeom*>(&(emcBank->getEntry(iS)));
00278 
00279       size_t nx, ny;
00280       double xs, ys;
00281 
00282       pdbsec->NxNy(nx, ny);
00283       sec.SetNxNy(nx, ny);
00284 
00285       pdbsec->TowerXYSize(xs, ys);
00286       sec.SetTowerXYSize(xs, ys);
00287 
00288       sec.SetDirectTransformation(pdbsec->RotationMatrix(),
00289                                   pdbsec->TranslationVector());
00290 
00291     } // iS
00292 
00293   geometry.Set(theSectors);
00294 
00295   bool status_commit = fBM->DBApplication()->commit();
00296   kIsRead = status_commit && kIsRead;
00297 
00298   delete emcBank;
00299 
00300   return kIsRead;
00301 }
00302 
00303 
00304 //_____________________________________________________________________________
00305 template<class BM>
00306 void
00307 emcOMGeometryT<BM>::Reset(void)
00308 {
00309   delete fGeometry;
00310   fGeometry = 0;
00311 }
00312 
00313 //_____________________________________________________________________________
00314 template<class BM>
00315 bool
00316 emcOMGeometryT<BM>::Write(const emcManageable& object,
00317                           const PHTimeStamp&,int)
00318 {
00319   bool kWritten = false;
00320 
00321   const emcGeometry& cgeometry = static_cast<const emcGeometry&>(object);
00322   emcGeometry& geometry = const_cast<emcGeometry&>(cgeometry);
00323 
00324   // Start a write transaction
00325   PdbStatus status = fBM->DBApplication()->startUpdate();
00326 
00327   if (!status)
00328     {
00329       if ( DM()->GetVerboseLevel() )
00330         {
00331           std::cerr << "<E> emcOMGeometryT<" << BM::name()
00332                     << ">::Write : cannot open a write "
00333                     << "transaction to Objy" << std::endl;
00334           fBM->DBApplication()->abort();
00335           return false;
00336         }
00337     }
00338 
00339   PdbCalBank* emcBank = 0;
00340   PdbBankID bankID(1);
00341   PHTimeStamp begin, end;
00342 
00343   std::string pdb_classname = "PdbEmcSecGeomBank";
00344   std::string description = "Written by emcOMGeometry";
00345   std::string basename = geometry.GetCategory();
00346   begin = geometry.GetStartValTime();
00347   end = geometry.GetEndValTime();
00348 
00349   // Here we must update all other banks valid at that time
00350   // to fix their end-of-validity time to calfem->GetStartValTime()-epsilon
00351   // >>
00352   PHTimeStamp the_new_end;
00353   the_new_end = begin;
00354   the_new_end -= 1;
00355 
00356   size_t nupdate = 0;
00357 
00358   PdbBankList bankList;
00359   fBM->BankManager()->fetchAllBanks(bankList, pdb_classname.c_str(), bankID, basename.
00360                                c_str(), begin);
00361   PdbBankListIterator iter(bankList);
00362   PdbCalBank* bank;
00363   while ( (bank = iter()) )
00364     {
00365       // we only update banks which are not the very same one
00366       bool same = (bank->getEndValTime() == end && bank->getStartValTime() == begin)
00367         ;
00368       if (!same)
00369         {
00370           assert (end >= bank->getEndValTime());
00371           // splitting of bank headers is not (yet) supported...
00372           bank->setEndValTime(the_new_end);
00373           nupdate++;
00374         }
00375     }
00376   if ( DM()->GetVerboseLevel() )
00377     {
00378       std::cout << "<I> emcOMGeometryT<" << BM::name() 
00379                 << ">::Write : " << nupdate
00380                 << " updated banks." << std::endl;
00381     }
00382   // <<
00383 
00384   emcBank = fBM->BankManager()->createBank(pdb_classname.c_str(),
00385                                            bankID,
00386                                            description.c_str(),
00387                                            begin, end,
00388                                            basename.c_str());
00389 
00390   assert(emcBank != 0);
00391 
00392   // emcBank is a set of PdbEmcSecGeom :
00393 
00394   emcBank->setLength(geometry.NumberOfSectors());
00395 
00396   PdbEmcSecGeom* pdbsec;
00397 
00398   for ( size_t iS = 0; iS < geometry.NumberOfSectors(); iS++ )
00399     {
00400       const SecGeom& sec = geometry.Sector(iS);
00401 
00402       pdbsec = static_cast<PdbEmcSecGeom*>(&(emcBank->getEntry(iS)));
00403 
00404       pdbsec->SetNxNy(sec.nx(), sec.ny());
00405       pdbsec->SetTowerXYSize(sec.Tower_xSize(), sec.Tower_ySize());
00406       const PHMatrix& mat = sec.RotationMatrix();
00407       const PHVector& vec = sec.TranslationVector();
00408       pdbsec->SetTransformation(mat, vec);
00409 
00410     } // iS
00411 
00412   status = fBM->DBApplication()->commit();
00413 
00414   if (status)
00415     {
00416       kWritten = true;
00417     }
00418   else
00419     {
00420       std::cerr << "<E> emcOMGeometryT<" << BM::name() 
00421                 << ">::Write : commit failed ?!" << std::endl;
00422       kWritten = false;
00423     }
00424 
00425   delete emcBank;
00426 
00427   return kWritten;
00428 }
00429 
00430 #endif