emcOMTracedFEMT.h

Go to the documentation of this file.
00001 #ifndef __EMCOMTRACEDFEM_H__
00002 #define __EMCOMTRACEDFEM_H__
00003 
00004 #ifndef __EMCOMCALFEMT_H__
00005 #include "emcOMCalFEMT.h"
00006 #endif
00007 
00008 class emcTracedFEM;
00009 
00015 template <class T, class B>
00016 class emcOMTracedFEMT : public emcOMCalFEMT<T>
00017 {
00018 
00019 public:
00020   emcOMTracedFEMT(const char* name, const char* title) :
00021     emcOMCalFEMT<T>(name, title)
00022   { }
00023 
00025   virtual bool CanRead(const emcManageable& object) const;
00026 
00028   virtual bool CanWrite(const emcManageable& object) const;
00029 
00031   virtual void FromPdbCalBank(emcCalFEM& calfem, PdbCalBank& bank);
00032 
00034   virtual std::string GetPersistentClassName(void) const
00035   {
00036     return "PdbEmcTracedValueBank";
00037   }
00038 
00040   virtual void ToPdbCalBank(const emcCalFEM& calfem, PdbCalBank& bank);
00041 
00042 };
00043 
00044 //_____________________________________________________________________________
00045 //_____________________________________________________________________________
00046 //_____________________________________________________________________________
00047 
00048 #include "PdbCalBank.hh"
00049 #include "PdbEmcTracedValue.hh"
00050 
00051 #ifndef __EMCINDEXER_H__
00052 #include "EmcIndexer.h"
00053 #endif
00054 #ifndef __EMCTRACEDVALUE_H__
00055 #include "emcTracedValue.h"
00056 #endif
00057 
00058 #include <cassert>
00059 #include <ctime>
00060 #include <string>
00061 #include <fstream>
00062 
00063 //_____________________________________________________________________________
00064 template <class T, class B>
00065 bool
00066 emcOMTracedFEMT<T,B>::CanRead(const emcManageable& object) const
00067 {
00068   if ( object.GetSource() != storage() )
00069     {
00070       return false;
00071     }
00072 
00073   const emcManageable* object_ptr = &object;
00074   const B* test = dynamic_cast<const B*>(object_ptr);
00075 
00076   if (test)
00077     {
00078       std::string test_category = B(0).GetCategory();
00079       if ( test_category == object.GetCategory() )
00080         {
00081           return true;
00082         }
00083       else
00084         {
00085           return false;
00086         }
00087     }
00088 
00089   return false;
00090 }
00091 
00092 //_____________________________________________________________________________
00093 template <class T, class B>
00094 bool
00095 emcOMTracedFEMT<T,B>::CanWrite(const emcManageable& object) const
00096 {
00097   if ( object.GetDestination() != storage() )
00098     {
00099       return false;
00100     }
00101 
00102   const emcManageable* object_ptr = &object;
00103   const B* test = dynamic_cast<const B*>(object_ptr);
00104 
00105   if (test)
00106     {
00107       std::string test_category = B(0).GetCategory();
00108       if ( test_category == object.GetCategory() )
00109         {
00110           return true;
00111         }
00112       else
00113         {
00114           return false;
00115         }
00116     }
00117 
00118   return false;
00119 }
00120 
00121 //_____________________________________________________________________________
00122 template<class T, class B>
00123 void
00124 emcOMTracedFEMT<T,B>::FromPdbCalBank(emcCalFEM& calfem, PdbCalBank& bank)
00125 {
00126   // Fills calfem from the values in bank.
00127   //
00128   // Max. number of channels handled by this method = 144.
00129   // (as channel#=144 and 145 do have special meaning).
00130   //
00131   // thesize take into account normal items + 2
00132   // (xmin and xmax)
00133 
00134   B& tracedFEM = dynamic_cast<B&>(calfem);
00135 
00136   int nitems = 0;
00137   size_t nchannels = 0;
00138   size_t thesize = bank.getLength();
00139   int j = 0, lastj = -1;
00140   int thex;
00141   float constant, slope;
00142   PdbEmcTracedValue* pdbtv;
00143 
00144   // find the number of items and number of channels.
00145   for ( size_t i = 0; i < thesize; i++ )
00146     {
00147       pdbtv = (PdbEmcTracedValue*) & (bank.getEntry(i));
00148       assert(pdbtv != 0);
00149       pdbtv->Get(j, thex, constant, slope);
00150       if (j < 144)
00151         {
00152           nitems++;
00153           if ( j > lastj )
00154             nchannels = j;
00155         }
00156       lastj = j;
00157     }
00158 
00159   nchannels++;
00160 
00161   int extra = thesize - nitems;
00162 
00163   tracedFEM.SetNumberOfChannels(nchannels);
00164   int n = 0;
00165 
00166   // We loop over the bank to extract data.
00167   for ( int i = 0; i < nitems; i++ )
00168     {
00169       pdbtv = (PdbEmcTracedValue*) & (bank.getEntry(i));
00170       pdbtv->Get(j, thex, constant, slope);
00171       tracedFEM.AddNewItem(j, new emcTracedValue(thex, constant, slope));
00172       n++;
00173     }
00174 
00175   // Last line of the bank is used for storing xmax (and xmin))
00176 
00177   assert(extra == 1 || extra == 2);
00178 
00179   pdbtv = (PdbEmcTracedValue*) & (bank.getEntry(n));
00180   assert(pdbtv != 0);
00181   pdbtv->Get(j, thex, constant, slope);
00182   assert(constant == 0);
00183   assert(slope == 0);
00184 
00185   phtime_t xmax = thex;
00186 
00187   if (extra == 2)
00188     {
00189       // new version where both xmin and xmax are stored.
00190       n++;
00191       pdbtv = (PdbEmcTracedValue*) & (bank.getEntry(n));
00192       assert(pdbtv != 0);
00193       pdbtv->Get(j, thex, constant, slope);
00194       assert(constant == 0);
00195       assert(slope == 0);
00196       calfem.SetXmin(thex);
00197       calfem.SetXmax(xmax);
00198     }
00199   else
00200     {
00201       // old version where only xmax was put in the db.
00202       // and xmax was a relative one !
00203       if ( EmcIndexer::isPbScFEM(calfem.AbsolutePosition()) )
00204         {
00205           phtime_t tics0 = calfem.GetStartValTime().getTics();
00206           calfem.SetXmin(tics0);
00207           calfem.SetXmax(xmax + tics0);
00208         }
00209     }
00210 }
00211 
00212 //_____________________________________________________________________________
00213 template<class T, class B>
00214 void
00215 emcOMTracedFEMT<T,B>::ToPdbCalBank(const emcCalFEM& calfem, PdbCalBank& bank)
00216 {
00217   int nvalues = 0;
00218   const B& ctracedFEM = static_cast<const B&>(calfem);
00219   B& tracedFEM = const_cast<B&>(ctracedFEM);
00220 
00221   nvalues += tracedFEM.GetNumberOfItems();
00222 
00223   /* The bank will in fact has size nvalues+2, the last values
00224      being used only to store the xmax and xmin of this object.
00225   */
00226 
00227   bank.setLength(nvalues + 2);
00228 
00229   // We then fill the bank
00230 
00231   PdbEmcTracedValue* pdbtv;
00232   emcTracedValue* val;
00233   int n = 0;
00234 
00235   for ( size_t i = 0; i < tracedFEM.GetNumberOfChannels(); i++ )
00236     {
00237       tracedFEM.FirstItem(i);
00238       while ( (val = tracedFEM.NextItem()) != 0 )
00239         {
00240           pdbtv = (PdbEmcTracedValue*) & (bank.getEntry(n));
00241           assert(pdbtv != 0);
00242           pdbtv->Set(i, val->GetX(), val->GetConstant(), val->GetSlope());
00243           n++;
00244         }
00245     }
00246 
00247   pdbtv = (PdbEmcTracedValue*) & (bank.getEntry(n));
00248   assert(pdbtv != 0);
00249   pdbtv->Set(tracedFEM.GetNumberOfChannels(), tracedFEM.GetXmax(), 0, 0);
00250   n++;
00251   pdbtv = (PdbEmcTracedValue*) & (bank.getEntry(n));
00252   assert(pdbtv != 0);
00253   pdbtv->Set(tracedFEM.GetNumberOfChannels() + 1, tracedFEM.GetXmin(), 0, 0);
00254 }
00255 
00256 #endif