CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/CondFormats/CastorObjects/interface/CastorCondObjectContainer.h

Go to the documentation of this file.
00001 #ifndef CastorCondObjectContainer_h
00002 #define CastorCondObjectContainer_h
00003 //
00004 //Adapted for CASTOR by L. Mundim
00005 //
00006 #include <iostream>
00007 #include <vector>
00008 #include "DataFormats/DetId/interface/DetId.h"
00009 #include "DataFormats/HcalDetId/interface/HcalCastorDetId.h"
00010 #include "FWCore/Utilities/interface/Exception.h"
00011 #include <cstdlib>
00012 
00013 template<class Item>
00014 class CastorCondObjectContainer
00015 {
00016  public:
00017   // default constructor
00018   CastorCondObjectContainer();
00019 
00020   // destructor:
00021   ~CastorCondObjectContainer();
00022 
00023   // get the object back
00024   const Item* getValues(DetId fId, bool throwOnFail=true) const;
00025 
00026   // does the object exist ?
00027   const bool exists(DetId fId) const;
00028 
00029   // set the object/fill it in:
00030   bool addValues(const Item& myItem);
00031   //bool addValues(const Item& myItem, bool h2mode_=false);
00032 
00033   // list of available channels:
00034   std::vector<DetId> getAllChannels() const;
00035 
00036   std::string myname() const {return (std::string)"Castor Undefined";}
00037 
00038  private:
00039   void initContainer();
00040   unsigned int hashed_id(DetId fId) const;
00041 
00042   std::vector<Item> CASTORcontainer;
00043 };
00044 
00045 
00046 template<class Item>
00047 //CastorCondObjectContainer<Item>::CastorCondObjectContainer(): m_h2mode(false)
00048 CastorCondObjectContainer<Item>::CastorCondObjectContainer()
00049 {
00050 }
00051 
00052 template<class Item>
00053 CastorCondObjectContainer<Item>::~CastorCondObjectContainer()
00054 {
00055 }
00056 
00057 template<class Item> void
00058 CastorCondObjectContainer<Item>::initContainer()
00059 {
00060   Item emptyItem;
00061 
00062   if (CASTORcontainer.empty())
00063     for (int i=0; i<HcalCastorDetId:: kSizeForDenseIndexing; i++)
00064       CASTORcontainer.push_back(emptyItem);
00065   
00066 }
00067 
00068 
00069 template<class Item> const Item*
00070 CastorCondObjectContainer<Item>::getValues(DetId fId, bool throwOnFail) const
00071 {
00072   const Item* cell = NULL;
00073   HcalCastorDetId myId(fId);
00074 
00075   if (fId.det()==DetId::Calo && fId.subdetId()==HcalCastorDetId::SubdetectorId) {
00076     unsigned int index = hashed_id(fId);
00077     
00078     if (index < CASTORcontainer.size()) 
00079         cell = &(CASTORcontainer.at(index) ); 
00080   }
00081 
00082  
00083   if ((!cell) || (cell->rawId() != fId ) ) {
00084     if (throwOnFail) {
00085       throw cms::Exception ("Conditions not found") 
00086         << "Unavailable Conditions of type " << myname() << " for cell " << myId;
00087     } else {
00088       cell=0;
00089     }
00090   }
00091   return cell;
00092 }
00093 
00094 template<class Item> const bool
00095 CastorCondObjectContainer<Item>::exists(DetId fId) const
00096 {
00097   const Item* cell = getValues(fId,false);
00098   if (cell)
00099     //    if (cell->rawId() != emptyItem.rawId() ) 
00100     if (cell->rawId() == fId ) 
00101       return true;
00102   return false;
00103 }
00104 
00105 template<class Item> bool
00106 CastorCondObjectContainer<Item>::addValues(const Item& myItem)
00107 {
00108   unsigned long myRawId = myItem.rawId();
00109   HcalCastorDetId myId(myRawId);
00110   unsigned int index = hashed_id(myId);
00111   bool success = false;
00112 
00113 
00114   if (CASTORcontainer.empty() ) initContainer();
00115   if (index < CASTORcontainer.size())
00116     {
00117       CASTORcontainer.at(index)  = myItem; 
00118       success = true;
00119     }
00120   
00121 
00122   if (!success) 
00123     throw cms::Exception ("Filling of conditions failed") 
00124       << " no valid filling possible for Conditions of type " << myname() << " for DetId " << myId;
00125   
00126   return success;
00127 }
00128 
00129 template<class Item> std::vector<DetId>
00130 CastorCondObjectContainer<Item>::getAllChannels() const
00131 {
00132   std::vector<DetId> channels;
00133   Item emptyItem;
00134   for (unsigned int i=0; i<CASTORcontainer.size(); i++)
00135     {
00136       if (emptyItem.rawId() != CASTORcontainer.at(i).rawId() )
00137         channels.push_back( DetId(CASTORcontainer.at(i).rawId()) );
00138     }
00139 
00140   return channels;
00141 }
00142 
00143 
00144 template<class Item>
00145 unsigned int CastorCondObjectContainer<Item>::hashed_id(DetId fId) const {
00146   // the historical packing from HcalGeneric is different from HcalCastorDetId, so we clone the old packing here.
00147   HcalCastorDetId tid(fId); 
00148   int zside = tid.zside();
00149   int sector = tid.sector();
00150   int module = tid.module(); 
00151   static const int CASTORhalf=224;
00152   
00153   int index = 14*(sector-1) + (module-1);
00154   if (zside == -1) index += CASTORhalf;
00155   
00156   return index;
00157 }
00158 
00159 #endif