emcOMFEMtupleT.h

Go to the documentation of this file.
00001 #ifndef __EMCOMFEMTUPLET_H__
00002 #define __EMCOMFEMTUPLET_H__
00003 
00004 #include <string>
00005 #ifndef __EMCMANAGEABLE_H__
00006 #include "emcManageable.h"
00007 #endif
00008 #ifndef __EMCOBJETMANAGER_H__
00009 #include "emcObjectManager.h"
00010 #endif
00011 
00012 class emcFEMtuple;
00013 class emcCalFEM;
00014 class PdbCalBank;
00015 class PdbBankID;
00016 
00038 template <class BM, class FT>
00039 class emcOMFEMtupleT : public emcObjectManager
00040 {
00041 public:
00042   emcOMFEMtupleT(const char* name = "", const char* title = "");
00043   virtual ~emcOMFEMtupleT();
00044 
00046   virtual bool CanCollect(const emcManageable& object) const
00047   { return CanRead(object); }
00048 
00050   virtual bool CanRead(const emcManageable& object) const;
00051 
00053   virtual bool CanWrite(const emcManageable& object) const;
00054     
00056   virtual emcManageable* Collect(const emcManageable& object,
00057                                  const PHTimeStamp& when);
00058 
00059   using emcObjectManager::Read;
00060 
00062   virtual bool Read(emcManageable& object,
00063                     const PHTimeStamp& time_stamp,
00064                     int femCode);
00065 
00067   virtual void Reset(void);
00068 
00070   virtual bool Write(const emcManageable& object,
00071                      const PHTimeStamp& tdummy,
00072                      int dummy = -1);
00073 
00074 private:
00075 
00076   class changeName
00077   {
00078   public:
00079     changeName(const char* name)
00080     {
00081       name_ = emcManageable::GetStorageName(BM::storage());
00082       name_ += ":Forwarder:";
00083       name_ += name;
00084     }
00085 
00086     const char* c_str() const
00087     {
00088       return name_.c_str();
00089     }
00090 
00091   private:
00092     std::string name_;
00093   };
00094 
00095 protected:
00096   FT* fFEMs;
00097   BM* fBM;
00098 };
00099 
00100 //_____________________________________________________________________________
00101 //_____________________________________________________________________________
00102 //_____________________________________________________________________________
00103 
00104 #ifndef __EMCRAWDATAACCESSOR_H__
00105 #include "emcRawDataAccessor.h"
00106 #endif
00107 #ifndef __EMCFEMTUPLEFACTORY_H__
00108 #include "emcFEMtupleFactory.h"
00109 #endif
00110 #ifndef __EMCDATAMANAGER_H__
00111 #include "emcDataManager.h"
00112 #endif
00113 #ifndef __EMCDEFINES_H__
00114 #include "emcDefines.h"
00115 #endif
00116 #ifndef __EMCCALFEM_H__
00117 #include "emcCalFEM.h"
00118 #endif
00119 #ifndef __EMCCALFEMFACTORY_H__
00120 #include "emcCalFEMFactory.h"
00121 #endif
00122 #ifndef __EMCDYNAMICDATA_H__
00123 #include "EmcDynamicData.h"
00124 #endif
00125 #ifndef __EMCFEMTUPLE_H__
00126 #include "emcFEMtuple.h"
00127 #endif
00128 #ifndef __EMCFEMTUPLEFACTORY_H__
00129 #include "emcFEMtupleFactory.h"
00130 #endif
00131 #ifndef __EMCOBJECTMANAGERREGISTER_H__
00132 #include "emcObjectManagerRegister.h"
00133 #endif
00134 #ifndef __EMCOMHELPER_H__
00135 #include "emcOMHelper.h"
00136 #endif
00137 
00138 //_____________________________________________________________________________
00139 template <class BM, class FT>
00140 emcOMFEMtupleT<BM,FT>::emcOMFEMtupleT(const char* name, const char* title)
00141   : emcObjectManager(changeName(name).c_str(), title),
00142     fFEMs(0),
00143     fBM(new BM)
00144 {}
00145 
00146 //_____________________________________________________________________________
00147 template <class BM, class FT>
00148 emcOMFEMtupleT<BM,FT>::~emcOMFEMtupleT()
00149 {
00150   Reset();
00151 }
00152 
00153 //_____________________________________________________________________________
00154 template <class BM, class FT>
00155 bool
00156 emcOMFEMtupleT<BM,FT>::CanRead(const emcManageable& object) const
00157 {
00158   if ( object.GetSource() != fBM->storage() )
00159     {
00160       return false;
00161     }
00162   const FT* test = dynamic_cast<const FT*>(&object);
00163   if ( test )
00164     {
00165       emcCalFEM* cal = emcCalFEMFactory::Create(test->GetCategory(),0);
00166       cal->SetSource(test->GetSource());
00167       emcObjectManager* plug = emcOMHelper::findOM(*cal,0);
00168       delete cal;
00169       if ( plug ) 
00170         {
00171           return true;
00172         }
00173     }
00174   return false;
00175 }
00176 
00177 //_____________________________________________________________________________
00178 template <class BM, class FT>
00179 bool
00180 emcOMFEMtupleT<BM,FT>::CanWrite(const emcManageable& object) const
00181 {
00182   if ( object.GetDestination() != fBM->storage() )
00183     {
00184       return false;
00185     }
00186   const FT* test = dynamic_cast<const FT*>(&object);
00187   if ( test )
00188     {
00189       emcCalFEM* cal = emcCalFEMFactory::Create(test->GetCategory(),0);
00190       cal->SetDestination(test->GetDestination());
00191       emcObjectManager* plug = emcOMHelper::findOM(*cal,1);
00192       delete cal;
00193       if ( plug ) 
00194         {
00195           return true;
00196         }
00197     }
00198   return false;
00199 }
00200 
00201 //_____________________________________________________________________________
00202 template <class BM, class FT>
00203 emcManageable*
00204 emcOMFEMtupleT<BM,FT>::Collect(const emcManageable& object,
00205                            const PHTimeStamp& when)
00206 {
00207 
00208   if ( object.GetSource() != fBM->storage() )
00209     {
00210       std::cerr << "emcOMFEMtupleT<" << BM::name() <<">::Collect : "
00211                 << "object source is not compatible"
00212                 << std::endl;
00213       return 0;
00214     }
00215 
00216   if (fFEMs)
00217     {
00218       // If what we have already is still valid, returns it.
00219       if (fFEMs->IsValid(when))
00220         {
00221           return fFEMs;
00222         }
00223     }
00224 
00225   if ( DM()->GetVerboseLevel() )
00226     {
00227       std::cout << "<I-EMCAL> Fetching " << object.GetCategory() << " from "
00228                 << emcManageable::GetStorageName(object.GetSource()) 
00229                 << std::endl;
00230     }
00231 
00232   emcRawDataAccessor* rda = emcRawDataAccessor::GetInstance();
00233   assert(rda != 0);
00234   int nSM = rda->GetDynamicData()->getnSM();
00235   const SuperModule* SMMap = rda->GetDynamicData()->getSMMap();
00236 
00237   bool kEmpty = false;
00238 
00239   if (!fFEMs)
00240     {
00241       fFEMs = static_cast<FT*>
00242         (emcFEMtupleFactory::Create(object.GetCategory()));
00243       assert(fFEMs != 0);
00244       kEmpty = true;
00245     }
00246   else
00247     {
00248       // Check that the data source has not changed
00249       if (fFEMs->GetSource() != object.GetSource())
00250         {
00251           // Data source has changed. Just forget what we have.
00252           delete fFEMs;
00253           fFEMs = static_cast<FT*>
00254             (emcFEMtupleFactory::Create(object.GetCategory()));
00255           assert(fFEMs != 0);
00256           kEmpty = true;
00257         }
00258     }
00259 
00260   // Tells from where we get the data.
00261   fFEMs->SetSource(object.GetSource());
00262 
00263   int ifem; // fem index
00264   int code;
00265   bool ok = true;
00266   emcFEMtuple* oneFEM;
00267 
00268   // Loop over FEMs
00269 
00270   for ( ifem = 0; ifem < nSM && ok == true; ifem++ )
00271     {
00272 
00273       // Skip the monitoring crates (if any) for anything which is
00274       // not Pedestals or HLRatios
00275       // -- Corrected Oct. 3, 2000 by HKD
00276       // Now skip the monitoring crates (if any) for Gains ONLY
00277       const char* category = object.GetCategory();
00278       assert(category != 0);
00279       if ( (strcmp(category, "Gains") == 0 ||
00280             strcmp(category, "TofT0s") == 0) &&
00281            SMMap[ifem].absPosition >= 172 )
00282         break;
00283 
00284       oneFEM = emcFEMtupleFactory::Create(object.GetCategory());
00285       assert (oneFEM != 0);
00286       oneFEM->SetSource(object.GetSource());
00287 
00288       code = emcCalFEM::FEMCode(SMMap[ifem].absPosition, SMMap[ifem].femPin,
00289                                 SMMap[ifem].post_pre, SMMap[ifem].tac_pre);
00290 
00291       if (kEmpty)
00292         {
00293           // First time we are filling fFEMs : we read all the fems.
00294           ok = Read((*oneFEM), when, code);
00295           if (ok)
00296             {
00297               ok = fFEMs->Add((*oneFEM));
00298               if (!ok)
00299                 {
00300                   std::cerr << "<E-EMCAL> emcOMFEMtupleT::Collect : "
00301                             << "Add failed." << std::endl;
00302                   break;
00303                 }
00304               else
00305                 {
00306                   if (DM()->GetVerboseLevel())
00307                     {
00308                       oneFEM->Print();
00309                     }
00310                 }
00311             }
00312           else
00313             {
00314               std::cerr << "<E-EMCAL> emcOMFEMtupleT::Collect : "
00315                         << "Read failed for fem " << ifem 
00316                         << std::endl;
00317             }
00318         }
00319       else
00320         {
00321           // fFEMs is already filled up, se we first check
00322           // that we really need to fetch new values for this FEM
00323           if ( ! fFEMs->IsValid(when, ifem) )
00324             {
00325               ok = Read((*oneFEM), when, code);
00326               if (ok)
00327                 {
00328                   if (DM()->GetVerboseLevel())
00329                     {
00330                       oneFEM->Print();
00331                     }
00332                   ok = fFEMs->ReplaceFEM(ifem, (*oneFEM));
00333                   if (!ok)
00334                     {
00335                       std::cerr << "<E-EMCAL> emcOMFEMtupleT::Collect : "
00336                                 << "ReplaceFEM failed. FEM is : " << std::endl;
00337                       oneFEM->Print();
00338                       break;
00339                     }
00340                 }
00341             }
00342         } // if (kEmpty)
00343 
00344       delete oneFEM;
00345 
00346     } // end loop over FEMs
00347 
00348   if (ok)
00349     {
00350       if ( DM()->GetVerboseLevel() )
00351         {
00352           std::cout << "done." << std::endl;
00353         }
00354       return fFEMs;
00355     }
00356   else
00357     {
00358       if ( DM()->GetVerboseLevel() )
00359         {
00360           std::cerr << "<E-EMCAL> emcOMFEMtupleT::Collect failed for "
00361                     << object.GetCategory() << std::endl;
00362         }
00363       delete fFEMs;
00364       fFEMs = 0;
00365       return 0;
00366     }
00367 }
00368 
00369 //_____________________________________________________________________________
00370 template <class BM, class FT>
00371 bool
00372 emcOMFEMtupleT<BM,FT>::Read(emcManageable& object,
00373                         const PHTimeStamp& time_stamp,
00374                         int femCode)
00375 {
00376 
00377   emcFEMtuple& femtuple = static_cast<emcFEMtuple&>(object);
00378   femtuple.Reset();
00379 
00380   int femAbsPosition, femPin, dummy;
00381   emcCalFEM::FEMDecode(femCode, femAbsPosition, femPin, dummy, dummy);
00382 
00383   PHTimeStamp tdummy(0);
00384 
00385   emcCalFEM* calfem = femtuple.AppendFEM(femAbsPosition, tdummy, tdummy);
00386   assert(calfem != 0);
00387   calfem->SetSource(object.GetSource());
00388 
00389   return DM()->Read(*calfem, time_stamp, femCode);
00390 }
00391 
00392 //_____________________________________________________________________________
00393 template <class BM, class FT>
00394 void
00395 emcOMFEMtupleT<BM,FT>::Reset(void)
00396 {
00397   delete fFEMs;
00398   fFEMs = 0;
00399 }
00400 
00401 //_____________________________________________________________________________
00402 template <class BM, class FT>
00403 bool
00404 emcOMFEMtupleT<BM,FT>::Write(const emcManageable& object,
00405                              const PHTimeStamp& /*tdummy*/,
00406                              int /*dummy*/)
00407 {
00408   // Write a manageable object to db.
00409   // object can handle any number of FEMs, we will split it in
00410   // different banks.
00411 
00412   bool kWritten = false;
00413 
00414   const emcFEMtuple& cfemtuple = static_cast<const emcFEMtuple&>(object);
00415   emcFEMtuple& femtuple = const_cast<emcFEMtuple&>(cfemtuple);
00416 
00417   // We loop over the fems to split the object
00418   for ( int ifem = 0; ifem < femtuple.GetNumberOfFEMs(); ifem++ )
00419     {
00420       // Get one FEM
00421       emcCalFEM* calfem = femtuple.GetFEM(ifem);
00422       assert (calfem != 0);
00423       if (DM()->GetVerboseLevel())
00424         {
00425           std::cout << "<I> emcOMFEMtupleT::Write : writing one FEM (flavour "
00426           << femtuple.GetCategory() << ")..." << std::endl;
00427           calfem->Print();
00428         }
00429 
00430       bool ok = DM()->Write(*calfem);
00431       if (!ok)
00432         {
00433           std::cerr << "<E> Could not write FEM " << ifem 
00434                     << "(flavour "
00435                     << femtuple.GetCategory() << ")..." << std::endl;     
00436         }
00437     }
00438 
00439   return kWritten;
00440 }
00441 
00442 #endif