emcOMTofT0FEMT.h

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