CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/CondFormats/HcalObjects/interface/HcalCondObjectContainer.h

Go to the documentation of this file.
00001 #ifndef HcalCondObjectContainer_h
00002 #define HcalCondObjectContainer_h
00003 
00004 #include <vector>
00005 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00006 #include "DataFormats/HcalDetId/interface/HcalOtherDetId.h"
00007 #include "DataFormats/HcalDetId/interface/HcalCastorDetId.h"
00008 #include "DataFormats/HcalDetId/interface/HcalZDCDetId.h"
00009 #include "FWCore/Utilities/interface/Exception.h"
00010 
00011 class HcalTopology;
00012 
00013 //#define HCAL_COND_SUPPRESS_DEFAULT
00014 
00015 class HcalCondObjectContainerBase {
00016 public:
00017   const HcalTopology* topo() const { return topo_; }
00018   int getCreatorPackedIndexVersion() const { return packedIndexVersion_; }
00019   void setTopo(const HcalTopology* topo) const;
00020   void setTopo(const HcalTopology* topo);
00021 protected:
00022   HcalCondObjectContainerBase(const HcalTopology*);
00023   unsigned int indexFor(DetId) const;
00024   unsigned int sizeFor(DetId) const;
00025   int packedIndexVersion_;
00026   inline HcalOtherSubdetector extractOther(const DetId& id) const { return HcalOtherSubdetector((id.rawId()>>20)&0x1F); }
00027   std::string textForId(const DetId& id) const;
00028 private:
00029   mutable const HcalTopology* topo_;
00030 };
00031 
00032 template<class Item>
00033 class HcalCondObjectContainer : public HcalCondObjectContainerBase {
00034 public:
00035   // default constructor
00036   HcalCondObjectContainer(const HcalTopology* topo) : HcalCondObjectContainerBase(topo) { }
00037 
00038   // destructor:
00039   virtual ~HcalCondObjectContainer();
00040 
00041   // get the object back
00042   const Item* getValues(DetId fId, bool throwOnFail=true) const;
00043 
00044   // does the object exist ?
00045   const bool exists(DetId fId) const;
00046 
00047   // set the object/fill it in:
00048   bool addValues(const Item& myItem);
00049 
00050   // list of available channels:
00051   std::vector<DetId> getAllChannels() const;
00052 
00053   virtual std::string myname() const {return (std::string)"Hcal Undefined";}
00054 
00055   // setting types for easier work for getAllContainers()
00056   typedef std::pair< std::string, std::vector<Item> > tHcalCont;
00057   typedef std::vector< tHcalCont > tAllContWithNames;
00058 
00059   const tAllContWithNames getAllContainers() const{
00060     tAllContWithNames allContainers;
00061     allContainers.push_back(tHcalCont("HB",HBcontainer));
00062     allContainers.push_back(tHcalCont("HE",HEcontainer));
00063     allContainers.push_back(tHcalCont("HO",HOcontainer));
00064     allContainers.push_back(tHcalCont("HF",HFcontainer));
00065     allContainers.push_back(tHcalCont("HT",HTcontainer));
00066     allContainers.push_back(tHcalCont("ZDC",ZDCcontainer));
00067     allContainers.push_back(tHcalCont("CALIB",CALIBcontainer));
00068     allContainers.push_back(tHcalCont("CASTOR",CASTORcontainer));
00069     return allContainers;
00070   }
00071 
00072  private:
00073   void initContainer(DetId container);
00074 
00075   std::vector<Item> HBcontainer;
00076   std::vector<Item> HEcontainer;
00077   std::vector<Item> HOcontainer;
00078   std::vector<Item> HFcontainer;
00079   std::vector<Item> HTcontainer;
00080   std::vector<Item> ZDCcontainer;
00081   std::vector<Item> CALIBcontainer;
00082   std::vector<Item> CASTORcontainer;
00083   
00084   //volatile const HcalTopology* topo_; // This needs to not be in the DB
00085 
00086 
00087 };
00088 
00089 
00090 template<class Item>
00091 HcalCondObjectContainer<Item>::~HcalCondObjectContainer()
00092 {
00093 }
00094 
00095 template<class Item> void
00096 HcalCondObjectContainer<Item>::initContainer(DetId fId)
00097 {
00098   Item emptyItem;
00099 
00100   if (fId.det()==DetId::Hcal) {
00101     switch (HcalSubdetector(fId.subdetId())) {
00102     case(HcalBarrel) : for (unsigned int i=0; i<sizeFor(fId); i++) HBcontainer.push_back(emptyItem); break;
00103     case(HcalEndcap) : for (unsigned int i=0; i<sizeFor(fId); i++) HEcontainer.push_back(emptyItem); break;
00104     case(HcalOuter) : for (unsigned int i=0; i<sizeFor(fId); i++) HOcontainer.push_back(emptyItem); break;
00105     case(HcalForward) : for (unsigned int i=0; i<sizeFor(fId); i++) HFcontainer.push_back(emptyItem); break;
00106     case(HcalTriggerTower) : for (unsigned int i=0; i<sizeFor(fId); i++) HTcontainer.push_back(emptyItem); break;
00107     case(HcalOther) : if (extractOther(fId)==HcalCalibration) 
00108         for (unsigned int i=0; i<sizeFor(fId); i++) CALIBcontainer.push_back(emptyItem); break;
00109       break; 
00110     default: break;
00111     }
00112   } else if (fId.det()==DetId::Calo) {
00113     if (fId.subdetId()==HcalCastorDetId::SubdetectorId) {
00114       for (unsigned int i=0; i<sizeFor(fId); i++) CASTORcontainer.push_back(emptyItem); 
00115     } else if (fId.subdetId()==HcalZDCDetId::SubdetectorId) {
00116       for (unsigned int i=0; i<sizeFor(fId); i++) ZDCcontainer.push_back(emptyItem); 
00117     }
00118   }
00119 }
00120 
00121 
00122 template<class Item> const Item*
00123 HcalCondObjectContainer<Item>::getValues(DetId fId, bool throwOnFail) const
00124 {
00125   unsigned int index=indexFor(fId);
00126   
00127   const Item* cell = NULL;
00128 
00129   if (index<0xFFFFFFFu) {
00130     if (fId.det()==DetId::Hcal) {
00131       switch (HcalSubdetector(fId.subdetId())) {
00132       case(HcalBarrel) : if (index < HBcontainer.size()) cell = &(HBcontainer.at(index) );  break;
00133       case(HcalEndcap) : if (index < HEcontainer.size()) cell = &(HEcontainer.at(index) );  break;
00134       case(HcalForward) : if (index < HFcontainer.size()) cell = &(HFcontainer.at(index) );   break; 
00135       case(HcalOuter) : if (index < HOcontainer.size()) cell = &(HOcontainer.at(index) );    break;
00136       case(HcalTriggerTower) : if (index < HTcontainer.size()) cell = &(HTcontainer.at(index) );    break;
00137       case(HcalOther) : if (extractOther(fId)==HcalCalibration) 
00138           if (index < CALIBcontainer.size()) cell = &(CALIBcontainer.at(index) );  
00139         break; 
00140       default: break;
00141       }
00142     } else if (fId.det()==DetId::Calo) {
00143       if (fId.subdetId()==HcalCastorDetId::SubdetectorId) {
00144         if (index < CASTORcontainer.size()) cell = &(CASTORcontainer.at(index) );
00145       } else if (fId.subdetId()==HcalZDCDetId::SubdetectorId) {
00146         if (index < ZDCcontainer.size()) cell = &(ZDCcontainer.at(index) );
00147       }
00148     }
00149   }
00150   
00151   //  Item emptyItem;
00152   //  if (cell->rawId() == emptyItem.rawId() ) 
00153   if ((!cell)) {
00154     if (throwOnFail) {
00155       throw cms::Exception ("Conditions not found") 
00156         << "Unavailable Conditions of type " << myname() << " for cell " << textForId(fId);
00157     } 
00158   } else if (cell->rawId() != fId) {
00159     if (throwOnFail) {
00160       throw cms::Exception ("Conditions mismatch") 
00161         << "Requested conditions of type " << myname() << " for cell " << textForId(fId) << " got conditions for cell " << textForId(DetId(cell->rawId()));
00162     } 
00163     cell=0;
00164   }
00165 
00166   return cell;
00167 }
00168 
00169 template<class Item> const bool
00170 HcalCondObjectContainer<Item>::exists(DetId fId) const
00171 {
00172   const Item* cell = getValues(fId,false);
00173 
00174   if (cell)
00175     if (cell->rawId() == fId ) 
00176       return true;
00177   
00178   return false;
00179 }
00180 
00181 template<class Item> bool
00182 HcalCondObjectContainer<Item>::addValues(const Item& myItem)
00183 {
00184   bool success = false;
00185   DetId fId(myItem.rawId());
00186   unsigned int index=indexFor(fId);
00187   
00188   Item* cell = NULL;
00189 
00190   if (index<0xFFFFFFFu) {
00191     if (fId.det()==DetId::Hcal) {
00192       switch (HcalSubdetector(fId.subdetId())) {
00193       case(HcalBarrel) : if (!HBcontainer.size() ) initContainer(fId);
00194         if (index < HBcontainer.size()) cell = &(HBcontainer.at(index) );  break;
00195       case(HcalEndcap) : if (!HEcontainer.size() ) initContainer(fId);
00196         if (index < HEcontainer.size()) cell = &(HEcontainer.at(index) );  break;
00197       case(HcalForward) : if (!HFcontainer.size() ) initContainer(fId);
00198         if (index < HFcontainer.size()) cell = &(HFcontainer.at(index) );  break;
00199       case(HcalOuter) : if (!HOcontainer.size() ) initContainer(fId);
00200         if (index < HOcontainer.size()) cell = &(HOcontainer.at(index) );  break;
00201       case(HcalTriggerTower) : if (!HTcontainer.size() ) initContainer(fId);
00202         if (index < HTcontainer.size()) cell = &(HTcontainer.at(index) );  break;  
00203       case(HcalOther) : if (extractOther(fId)==HcalCalibration) {
00204           if (!CALIBcontainer.size() ) initContainer(fId);
00205           if (index < CALIBcontainer.size()) cell = &(CALIBcontainer.at(index) );  
00206         }
00207         break; 
00208       default: break;
00209       }
00210     } else if (fId.det()==DetId::Calo) {
00211       if (fId.subdetId()==HcalCastorDetId::SubdetectorId) {
00212         if (!CASTORcontainer.size() ) initContainer(fId);
00213         if (index < CASTORcontainer.size()) cell = &(CASTORcontainer.at(index) );
00214       } else if (fId.subdetId()==HcalZDCDetId::SubdetectorId) { 
00215         if (!ZDCcontainer.size() ) initContainer(fId);
00216         if (index < ZDCcontainer.size()) cell = &(ZDCcontainer.at(index) );
00217       }
00218     }
00219   }
00220 
00221   if (cell!=0) {
00222     (*cell)=myItem;
00223     success=true;
00224   }
00225 
00226   if (!success) 
00227     throw cms::Exception ("Filling of conditions failed") 
00228       << " no valid filling possible for Conditions of type " << myname() << " for DetId " << textForId(fId);
00229   return success;
00230 }
00231 
00232 template<class Item> std::vector<DetId>
00233 HcalCondObjectContainer<Item>::getAllChannels() const
00234 {
00235   std::vector<DetId> channels;
00236   Item emptyItem;
00237   for (unsigned int i=0; i<HBcontainer.size(); i++)
00238     {
00239       if (emptyItem.rawId() != HBcontainer.at(i).rawId() )
00240         channels.push_back( DetId(HBcontainer.at(i).rawId()) );
00241     }
00242   for (unsigned int i=0; i<HEcontainer.size(); i++)
00243     {
00244       if (emptyItem.rawId() != HEcontainer.at(i).rawId() )
00245         channels.push_back( DetId(HEcontainer.at(i).rawId()) );
00246     }
00247   for (unsigned int i=0; i<HOcontainer.size(); i++)
00248     {
00249       if (emptyItem.rawId() != HOcontainer.at(i).rawId() )
00250         channels.push_back( DetId(HOcontainer.at(i).rawId()) );
00251     }
00252   for (unsigned int i=0; i<HFcontainer.size(); i++)
00253     {
00254       if (emptyItem.rawId() != HFcontainer.at(i).rawId() )
00255         channels.push_back( DetId(HFcontainer.at(i).rawId()) );
00256     }
00257   for (unsigned int i=0; i<HTcontainer.size(); i++)
00258     {
00259       if (emptyItem.rawId() != HTcontainer.at(i).rawId() )
00260         channels.push_back( DetId(HTcontainer.at(i).rawId()) );
00261     }
00262   for (unsigned int i=0; i<ZDCcontainer.size(); i++)
00263     {
00264       if (emptyItem.rawId() != ZDCcontainer.at(i).rawId() )
00265         channels.push_back( DetId(ZDCcontainer.at(i).rawId()) );
00266     }
00267   for (unsigned int i=0; i<CALIBcontainer.size(); i++)
00268     {
00269       if (emptyItem.rawId() != CALIBcontainer.at(i).rawId() )
00270         channels.push_back( DetId(CALIBcontainer.at(i).rawId()) );
00271     }
00272   for (unsigned int i=0; i<CASTORcontainer.size(); i++)
00273     {
00274       if (emptyItem.rawId() != CASTORcontainer.at(i).rawId() )
00275         channels.push_back( DetId(CASTORcontainer.at(i).rawId()) );
00276     }
00277 
00278   return channels;
00279 }
00280 
00281 
00282 #endif