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