Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:13

0001 #ifndef HcalCondObjectContainer_h
0002 #define HcalCondObjectContainer_h
0003 
0004 #include "CondFormats/Serialization/interface/Serializable.h"
0005 #include "CondFormats/HcalObjects/interface/HcalDetIdRelationship.h"
0006 
0007 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0008 #include "DataFormats/HcalDetId/interface/HcalOtherDetId.h"
0009 #include "DataFormats/HcalDetId/interface/HcalCastorDetId.h"
0010 #include "DataFormats/HcalDetId/interface/HcalZDCDetId.h"
0011 #include "FWCore/Utilities/interface/Exception.h"
0012 
0013 #include <vector>
0014 #include <string>
0015 
0016 class HcalTopology;
0017 
0018 //#define HCAL_COND_SUPPRESS_DEFAULT
0019 
0020 class HcalCondObjectContainerBase {
0021 public:
0022   const HcalTopology* topo() const { return topo_; }
0023   int getCreatorPackedIndexVersion() const { return packedIndexVersion_; }
0024   void setTopo(const HcalTopology* topo);
0025 
0026 protected:
0027   HcalCondObjectContainerBase(HcalCondObjectContainerBase const& o)
0028       : packedIndexVersion_(o.packedIndexVersion_), topo_(o.topo()) {}
0029   HcalCondObjectContainerBase& operator=(HcalCondObjectContainerBase const& o) {
0030     topo_ = o.topo();
0031     packedIndexVersion_ = o.packedIndexVersion_;
0032     return *this;
0033   }
0034 #ifndef __GCCXML__
0035   HcalCondObjectContainerBase(HcalCondObjectContainerBase&&) = default;
0036   HcalCondObjectContainerBase& operator=(HcalCondObjectContainerBase&&) = default;
0037 #endif
0038 
0039   HcalCondObjectContainerBase(const HcalTopology*);
0040   unsigned int indexFor(DetId) const;
0041   unsigned int sizeFor(DetId) const;
0042   int packedIndexVersion_ COND_TRANSIENT;
0043   inline HcalOtherSubdetector extractOther(const DetId& id) const {
0044     return HcalOtherSubdetector((id.rawId() >> 20) & 0x1F);
0045   }
0046   std::string textForId(const DetId& id) const;
0047 
0048 private:
0049   const HcalTopology* topo_ COND_TRANSIENT;
0050 
0051   COND_SERIALIZABLE;
0052 };
0053 
0054 template <class Item>
0055 class HcalCondObjectContainer : public HcalCondObjectContainerBase {
0056 public:
0057   // default constructor
0058   HcalCondObjectContainer(const HcalTopology* topo) : HcalCondObjectContainerBase(topo) {}
0059 
0060   // destructor:
0061   virtual ~HcalCondObjectContainer();
0062 
0063   // get the object back
0064   const Item* getValues(DetId fId, bool throwOnFail = true) const;
0065 
0066   // does the object exist ?
0067   bool exists(DetId fId) const;
0068 
0069   // set the object/fill it in:
0070   bool addValues(const Item& myItem);
0071 
0072   // list of available channels:
0073   std::vector<DetId> getAllChannels() const;
0074 
0075   virtual std::string myname() const { return (std::string) "Hcal Undefined"; }
0076 
0077   // setting types for easier work for getAllContainers()
0078   typedef std::pair<std::string, std::vector<Item> > tHcalCont;
0079   typedef std::vector<tHcalCont> tAllContWithNames;
0080 
0081   const tAllContWithNames getAllContainers() const {
0082     tAllContWithNames allContainers;
0083     allContainers.push_back(tHcalCont("HB", HBcontainer));
0084     allContainers.push_back(tHcalCont("HE", HEcontainer));
0085     allContainers.push_back(tHcalCont("HO", HOcontainer));
0086     allContainers.push_back(tHcalCont("HF", HFcontainer));
0087     allContainers.push_back(tHcalCont("HT", HTcontainer));
0088     allContainers.push_back(tHcalCont("ZDC", ZDCcontainer));
0089     allContainers.push_back(tHcalCont("CALIB", CALIBcontainer));
0090     allContainers.push_back(tHcalCont("CASTOR", CASTORcontainer));
0091     return allContainers;
0092   }
0093 
0094 private:
0095   void initContainer(DetId container);
0096 
0097   std::vector<Item> HBcontainer;
0098   std::vector<Item> HEcontainer;
0099   std::vector<Item> HOcontainer;
0100   std::vector<Item> HFcontainer;
0101   std::vector<Item> HTcontainer;
0102   std::vector<Item> ZDCcontainer;
0103   std::vector<Item> CALIBcontainer;
0104   std::vector<Item> CASTORcontainer;
0105 
0106   COND_SERIALIZABLE;
0107 };
0108 
0109 template <class Item>
0110 HcalCondObjectContainer<Item>::~HcalCondObjectContainer() {}
0111 
0112 template <class Item>
0113 void HcalCondObjectContainer<Item>::initContainer(DetId fId) {
0114   Item emptyItem;
0115 
0116   if (fId.det() == DetId::Hcal) {
0117     switch (HcalSubdetector(fId.subdetId())) {
0118       case HcalBarrel:
0119         for (unsigned int i = 0; i < sizeFor(fId); i++)
0120           HBcontainer.push_back(emptyItem);
0121         break;
0122       case HcalEndcap:
0123         for (unsigned int i = 0; i < sizeFor(fId); i++)
0124           HEcontainer.push_back(emptyItem);
0125         break;
0126       case HcalOuter:
0127         for (unsigned int i = 0; i < sizeFor(fId); i++)
0128           HOcontainer.push_back(emptyItem);
0129         break;
0130       case HcalForward:
0131         for (unsigned int i = 0; i < sizeFor(fId); i++)
0132           HFcontainer.push_back(emptyItem);
0133         break;
0134       case HcalTriggerTower:
0135         for (unsigned int i = 0; i < sizeFor(fId); i++)
0136           HTcontainer.push_back(emptyItem);
0137         break;
0138       case HcalOther:
0139         if (extractOther(fId) == HcalCalibration) {
0140           for (unsigned int i = 0; i < sizeFor(fId); i++)
0141             CALIBcontainer.push_back(emptyItem);
0142         }
0143         break;
0144       default:
0145         break;
0146     }
0147   } else if (fId.det() == DetId::Calo) {
0148     if (fId.subdetId() == HcalCastorDetId::SubdetectorId) {
0149       for (unsigned int i = 0; i < sizeFor(fId); i++)
0150         CASTORcontainer.push_back(emptyItem);
0151     } else if (fId.subdetId() == HcalZDCDetId::SubdetectorId) {
0152       for (unsigned int i = 0; i < sizeFor(fId); i++)
0153         ZDCcontainer.push_back(emptyItem);
0154     }
0155   }
0156 }
0157 
0158 template <class Item>
0159 const Item* HcalCondObjectContainer<Item>::getValues(DetId fId, bool throwOnFail) const {
0160   unsigned int index = indexFor(fId);
0161 
0162   const Item* cell = nullptr;
0163 
0164   if (index < 0xFFFFFFFu) {
0165     if (fId.det() == DetId::Hcal) {
0166       switch (HcalSubdetector(fId.subdetId())) {
0167         case HcalBarrel:
0168           if (index < HBcontainer.size())
0169             cell = &(HBcontainer.at(index));
0170           break;
0171         case HcalEndcap:
0172           if (index < HEcontainer.size())
0173             cell = &(HEcontainer.at(index));
0174           break;
0175         case HcalForward:
0176           if (index < HFcontainer.size())
0177             cell = &(HFcontainer.at(index));
0178           break;
0179         case HcalOuter:
0180           if (index < HOcontainer.size())
0181             cell = &(HOcontainer.at(index));
0182           break;
0183         case HcalTriggerTower:
0184           if (index < HTcontainer.size())
0185             cell = &(HTcontainer.at(index));
0186           break;
0187         case HcalOther:
0188           if (extractOther(fId) == HcalCalibration && index < CALIBcontainer.size())
0189             cell = &(CALIBcontainer.at(index));
0190           break;
0191         default:
0192           break;
0193       }
0194     } else if (fId.det() == DetId::Calo) {
0195       if (fId.subdetId() == HcalCastorDetId::SubdetectorId) {
0196         if (index < CASTORcontainer.size())
0197           cell = &(CASTORcontainer.at(index));
0198       } else if (fId.subdetId() == HcalZDCDetId::SubdetectorId) {
0199         if (index < ZDCcontainer.size())
0200           cell = &(ZDCcontainer.at(index));
0201       }
0202     }
0203   }
0204 
0205   if ((!cell)) {
0206     if (throwOnFail) {
0207       throw cms::Exception("Conditions not found")
0208           << "Unavailable Conditions of type " << myname() << " for cell " << textForId(fId);
0209     }
0210   } else if (!hcalEqualDetId(cell, fId)) {
0211     if (throwOnFail) {
0212       throw cms::Exception("Conditions mismatch")
0213           << "Requested conditions of type " << myname() << " for cell " << textForId(fId)
0214           << " got conditions for cell " << textForId(DetId(cell->rawId()));
0215     }
0216     cell = nullptr;
0217   }
0218 
0219   return cell;
0220 }
0221 
0222 template <class Item>
0223 bool HcalCondObjectContainer<Item>::exists(DetId fId) const {
0224   const Item* cell = getValues(fId, false);
0225 
0226   if (cell) {
0227     if (hcalEqualDetId(cell, fId))
0228       return true;
0229   }
0230   return false;
0231 }
0232 
0233 template <class Item>
0234 bool HcalCondObjectContainer<Item>::addValues(const Item& myItem) {
0235   bool success = false;
0236   DetId fId(myItem.rawId());
0237   unsigned int index = indexFor(fId);
0238 
0239   Item* cell = nullptr;
0240 
0241   if (index < 0xFFFFFFFu) {
0242     if (fId.det() == DetId::Hcal) {
0243       switch (HcalSubdetector(fId.subdetId())) {
0244         case HcalBarrel:
0245           if (HBcontainer.empty())
0246             initContainer(fId);
0247           if (index < HBcontainer.size())
0248             cell = &(HBcontainer.at(index));
0249           break;
0250         case HcalEndcap:
0251           if (HEcontainer.empty())
0252             initContainer(fId);
0253           if (index < HEcontainer.size())
0254             cell = &(HEcontainer.at(index));
0255           break;
0256         case HcalForward:
0257           if (HFcontainer.empty())
0258             initContainer(fId);
0259           if (index < HFcontainer.size())
0260             cell = &(HFcontainer.at(index));
0261           break;
0262         case HcalOuter:
0263           if (HOcontainer.empty())
0264             initContainer(fId);
0265           if (index < HOcontainer.size())
0266             cell = &(HOcontainer.at(index));
0267           break;
0268         case HcalTriggerTower:
0269           if (HTcontainer.empty())
0270             initContainer(fId);
0271           if (index < HTcontainer.size())
0272             cell = &(HTcontainer.at(index));
0273           break;
0274         case HcalOther:
0275           if (extractOther(fId) == HcalCalibration) {
0276             if (CALIBcontainer.empty())
0277               initContainer(fId);
0278             if (index < CALIBcontainer.size())
0279               cell = &(CALIBcontainer.at(index));
0280           }
0281           break;
0282         default:
0283           break;
0284       }
0285     } else if (fId.det() == DetId::Calo) {
0286       if (fId.subdetId() == HcalCastorDetId::SubdetectorId) {
0287         if (CASTORcontainer.empty())
0288           initContainer(fId);
0289         if (index < CASTORcontainer.size())
0290           cell = &(CASTORcontainer.at(index));
0291       } else if (fId.subdetId() == HcalZDCDetId::SubdetectorId) {
0292         if (ZDCcontainer.empty())
0293           initContainer(fId);
0294         if (index < ZDCcontainer.size())
0295           cell = &(ZDCcontainer.at(index));
0296       }
0297     }
0298   }
0299 
0300   if (cell != nullptr) {
0301     (*cell) = myItem;
0302     success = true;
0303   }
0304 
0305   if (!success)
0306     throw cms::Exception("Filling of conditions failed")
0307         << " no valid filling possible for Conditions of type " << myname() << " for DetId " << textForId(fId);
0308   return success;
0309 }
0310 
0311 template <class Item>
0312 std::vector<DetId> HcalCondObjectContainer<Item>::getAllChannels() const {
0313   std::vector<DetId> channels;
0314   Item emptyItem;
0315   for (unsigned int i = 0; i < HBcontainer.size(); i++) {
0316     if (emptyItem.rawId() != HBcontainer.at(i).rawId())
0317       channels.push_back(DetId(HBcontainer.at(i).rawId()));
0318   }
0319   for (unsigned int i = 0; i < HEcontainer.size(); i++) {
0320     if (emptyItem.rawId() != HEcontainer.at(i).rawId())
0321       channels.push_back(DetId(HEcontainer.at(i).rawId()));
0322   }
0323   for (unsigned int i = 0; i < HOcontainer.size(); i++) {
0324     if (emptyItem.rawId() != HOcontainer.at(i).rawId())
0325       channels.push_back(DetId(HOcontainer.at(i).rawId()));
0326   }
0327   for (unsigned int i = 0; i < HFcontainer.size(); i++) {
0328     if (emptyItem.rawId() != HFcontainer.at(i).rawId())
0329       channels.push_back(DetId(HFcontainer.at(i).rawId()));
0330   }
0331   for (unsigned int i = 0; i < HTcontainer.size(); i++) {
0332     if (emptyItem.rawId() != HTcontainer.at(i).rawId())
0333       channels.push_back(DetId(HTcontainer.at(i).rawId()));
0334   }
0335   for (unsigned int i = 0; i < ZDCcontainer.size(); i++) {
0336     if (emptyItem.rawId() != ZDCcontainer.at(i).rawId())
0337       channels.push_back(DetId(ZDCcontainer.at(i).rawId()));
0338   }
0339   for (unsigned int i = 0; i < CALIBcontainer.size(); i++) {
0340     if (emptyItem.rawId() != CALIBcontainer.at(i).rawId())
0341       channels.push_back(DetId(CALIBcontainer.at(i).rawId()));
0342   }
0343   for (unsigned int i = 0; i < CASTORcontainer.size(); i++) {
0344     if (emptyItem.rawId() != CASTORcontainer.at(i).rawId())
0345       channels.push_back(DetId(CASTORcontainer.at(i).rawId()));
0346   }
0347 
0348   return channels;
0349 }
0350 
0351 #endif