Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:53

0001 #ifndef CastorCondObjectContainer_h
0002 #define CastorCondObjectContainer_h
0003 //

0004 //Adapted for CASTOR by L. Mundim

0005 //

0006 #include "CondFormats/Serialization/interface/Serializable.h"
0007 
0008 #include <iostream>
0009 #include <vector>
0010 #include "DataFormats/DetId/interface/DetId.h"
0011 #include "DataFormats/HcalDetId/interface/HcalCastorDetId.h"
0012 #include "FWCore/Utilities/interface/Exception.h"
0013 #include <cstdlib>
0014 
0015 template <class Item>
0016 class CastorCondObjectContainer {
0017 public:
0018   // default constructor

0019   CastorCondObjectContainer();
0020 
0021   // destructor:

0022   ~CastorCondObjectContainer();
0023 
0024   // get the object back

0025   const Item* getValues(DetId fId, bool throwOnFail = true) const;
0026 
0027   // does the object exist ?

0028   const bool exists(DetId fId) const;
0029 
0030   // set the object/fill it in:

0031   bool addValues(const Item& myItem);
0032   //bool addValues(const Item& myItem, bool h2mode_=false);

0033 
0034   // list of available channels:

0035   std::vector<DetId> getAllChannels() const;
0036 
0037   std::string myname() const { return (std::string) "Castor Undefined"; }
0038 
0039 private:
0040   void initContainer();
0041   unsigned int hashed_id(DetId fId) const;
0042 
0043   std::vector<Item> CASTORcontainer;
0044 
0045   COND_SERIALIZABLE;
0046 };
0047 
0048 template <class Item>
0049 //CastorCondObjectContainer<Item>::CastorCondObjectContainer(): m_h2mode(false)

0050 CastorCondObjectContainer<Item>::CastorCondObjectContainer() {}
0051 
0052 template <class Item>
0053 CastorCondObjectContainer<Item>::~CastorCondObjectContainer() {}
0054 
0055 template <class Item>
0056 void CastorCondObjectContainer<Item>::initContainer() {
0057   Item emptyItem;
0058 
0059   if (CASTORcontainer.empty())
0060     for (int i = 0; i < HcalCastorDetId::kSizeForDenseIndexing; i++)
0061       CASTORcontainer.push_back(emptyItem);
0062 }
0063 
0064 template <class Item>
0065 const Item* CastorCondObjectContainer<Item>::getValues(DetId fId, bool throwOnFail) const {
0066   const Item* cell = nullptr;
0067   HcalCastorDetId myId(fId);
0068 
0069   if (fId.det() == DetId::Calo && fId.subdetId() == HcalCastorDetId::SubdetectorId) {
0070     unsigned int index = hashed_id(fId);
0071 
0072     if (index < CASTORcontainer.size())
0073       cell = &(CASTORcontainer.at(index));
0074   }
0075 
0076   if ((!cell) || (cell->rawId() != fId)) {
0077     if (throwOnFail) {
0078       throw cms::Exception("Conditions not found")
0079           << "Unavailable Conditions of type " << myname() << " for cell " << myId;
0080     } else {
0081       cell = nullptr;
0082     }
0083   }
0084   return cell;
0085 }
0086 
0087 template <class Item>
0088 const bool CastorCondObjectContainer<Item>::exists(DetId fId) const {
0089   const Item* cell = getValues(fId, false);
0090   if (cell)
0091     //    if (cell->rawId() != emptyItem.rawId() )

0092     if (cell->rawId() == fId)
0093       return true;
0094   return false;
0095 }
0096 
0097 template <class Item>
0098 bool CastorCondObjectContainer<Item>::addValues(const Item& myItem) {
0099   unsigned long myRawId = myItem.rawId();
0100   HcalCastorDetId myId(myRawId);
0101   unsigned int index = hashed_id(myId);
0102   bool success = false;
0103 
0104   if (CASTORcontainer.empty())
0105     initContainer();
0106   if (index < CASTORcontainer.size()) {
0107     CASTORcontainer.at(index) = myItem;
0108     success = true;
0109   }
0110 
0111   if (!success)
0112     throw cms::Exception("Filling of conditions failed")
0113         << " no valid filling possible for Conditions of type " << myname() << " for DetId " << myId;
0114 
0115   return success;
0116 }
0117 
0118 template <class Item>
0119 std::vector<DetId> CastorCondObjectContainer<Item>::getAllChannels() const {
0120   std::vector<DetId> channels;
0121   Item emptyItem;
0122   for (unsigned int i = 0; i < CASTORcontainer.size(); i++) {
0123     if (emptyItem.rawId() != CASTORcontainer.at(i).rawId())
0124       channels.push_back(DetId(CASTORcontainer.at(i).rawId()));
0125   }
0126 
0127   return channels;
0128 }
0129 
0130 template <class Item>
0131 unsigned int CastorCondObjectContainer<Item>::hashed_id(DetId fId) const {
0132   // the historical packing from HcalGeneric is different from HcalCastorDetId, so we clone the old packing here.

0133   HcalCastorDetId tid(fId);
0134   int zside = tid.zside();
0135   int sector = tid.sector();
0136   int module = tid.module();
0137   static const int CASTORhalf = 224;
0138 
0139   int index = 14 * (sector - 1) + (module - 1);
0140   if (zside == -1)
0141     index += CASTORhalf;
0142 
0143   return index;
0144 }
0145 
0146 #endif