Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:10:06

0001 #ifndef ContainerXXX_h
0002 #define ContainerXXX_h
0003 
0004 /*
0005  *      file:           ContainerXXX.h
0006  *      Author:         Viktor Khristenko
0007  *
0008  *      Description:
0009  */
0010 
0011 #include "DQM/HcalCommon/interface/Container1D.h"
0012 #include <cmath>
0013 
0014 namespace hcaldqm {
0015   typedef std::unordered_map<uint32_t, double> doubleCompactMap;
0016   typedef std::unordered_map<uint32_t, int> intCompactMap;
0017   typedef std::unordered_map<uint32_t, uint32_t> uintCompactMap;
0018 
0019   template <typename STDTYPE>
0020   class ContainerXXX {
0021   public:
0022     ContainerXXX() {}
0023     ContainerXXX(hashfunctions::HashType ht) : _hashmap(ht) {}
0024     ContainerXXX(ContainerXXX const &x);
0025     virtual ~ContainerXXX() { _cmap.clear(); }
0026 
0027     //  initialize, booking. booking is done from Electronicsmap.
0028     virtual void initialize(hashfunctions::HashType, int debug = 0);
0029     virtual void book(HcalElectronicsMap const *);
0030     virtual void book(HcalElectronicsMap const *, filter::HashFilter const &);
0031 
0032     //  setters
0033     virtual void set(HcalDetId const &, STDTYPE);
0034     virtual void set(HcalElectronicsId const &, STDTYPE);
0035     virtual void set(HcalTrigTowerDetId const &, STDTYPE);
0036 
0037     //  getters
0038     virtual STDTYPE &get(HcalDetId const &);
0039     virtual STDTYPE &get(HcalElectronicsId const &);
0040     virtual STDTYPE &get(HcalTrigTowerDetId const &);
0041 
0042     //  pushers/adders - not a push_back.
0043     //  ignored if already is present
0044     virtual void push(HcalDetId const &, STDTYPE);
0045     virtual void push(HcalElectronicsId const &, STDTYPE);
0046     virtual void push(HcalTrigTowerDetId const &, STDTYPE);
0047 
0048     //  finders, note true/false - no pointer to the actual guy...
0049     //  I know, I know not the best coding...
0050     virtual bool exists(HcalDetId const &);
0051     virtual bool exists(HcalElectronicsId const &);
0052     virtual bool exists(HcalTrigTowerDetId const &);
0053 
0054     virtual void dump(Container1D *);
0055     virtual void dump(std::vector<Container1D *> const &);
0056 
0057     virtual void load(Container1D *);
0058     virtual void reset();
0059     virtual uint32_t size();
0060     virtual void print();
0061 
0062   protected:
0063     typedef std::unordered_map<uint32_t, STDTYPE> CompactMap;
0064     CompactMap _cmap;
0065     mapper::HashMapper _hashmap;
0066     Logger _logger;
0067 
0068   public:
0069     virtual typename CompactMap::const_iterator begin() { return _cmap.begin(); }
0070     virtual typename CompactMap::const_iterator end() { return _cmap.end(); }
0071   };
0072 
0073   template <typename STDTYPE>
0074   ContainerXXX<STDTYPE>::ContainerXXX(ContainerXXX const &x) {
0075     for (auto &p : _cmap) {
0076       _cmap.insert(std::make_pair(p.first, p.second));
0077     }
0078   }
0079 
0080   template <typename STDTYPE>
0081   void ContainerXXX<STDTYPE>::initialize(hashfunctions::HashType ht, int debug) {
0082     _hashmap.initialize(ht);
0083     _logger.set("XXX", debug);
0084   }
0085 
0086   template <typename STDTYPE>
0087   void ContainerXXX<STDTYPE>::book(HcalElectronicsMap const *emap) {
0088     if (_hashmap.isDHash()) {
0089       std::vector<HcalGenericDetId> dids = emap->allPrecisionId();
0090       for (std::vector<HcalGenericDetId>::const_iterator it = dids.begin(); it != dids.end(); ++it) {
0091         if (!it->isHcalDetId())
0092           continue;
0093 
0094         HcalDetId did = HcalDetId(it->rawId());
0095         uint32_t hash = _hashmap.getHash(did);
0096         _logger.debug(_hashmap.getName(did));
0097         typename CompactMap::iterator mit = _cmap.find(hash);
0098         if (mit != _cmap.end())
0099           continue;
0100 
0101         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0102       }
0103     } else if (_hashmap.isEHash()) {
0104       std::vector<HcalElectronicsId> eids = emap->allElectronicsIdPrecision();
0105       for (std::vector<HcalElectronicsId>::const_iterator it = eids.begin(); it != eids.end(); ++it) {
0106         HcalElectronicsId eid = HcalElectronicsId(it->rawId());
0107         uint32_t hash = _hashmap.getHash(eid);
0108         _logger.debug(_hashmap.getName(eid));
0109         typename CompactMap::iterator mit = _cmap.find(hash);
0110         if (mit != _cmap.end())
0111           continue;
0112 
0113         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0114       }
0115     } else if (_hashmap.isTHash()) {
0116       std::vector<HcalTrigTowerDetId> tids = emap->allTriggerId();
0117       for (std::vector<HcalTrigTowerDetId>::const_iterator it = tids.begin(); it != tids.end(); ++it) {
0118         HcalTrigTowerDetId tid = HcalTrigTowerDetId(it->rawId());
0119         uint32_t hash = _hashmap.getHash(tid);
0120         _logger.debug(_hashmap.getName(tid));
0121         typename CompactMap::iterator mit = _cmap.find(hash);
0122         if (mit != _cmap.end())
0123           continue;
0124 
0125         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0126       }
0127     }
0128   }
0129 
0130   template <typename STDTYPE>
0131   void ContainerXXX<STDTYPE>::book(HcalElectronicsMap const *emap, filter::HashFilter const &filter) {
0132     if (_hashmap.isDHash()) {
0133       std::vector<HcalGenericDetId> dids = emap->allPrecisionId();
0134       for (std::vector<HcalGenericDetId>::const_iterator it = dids.begin(); it != dids.end(); ++it) {
0135         if (!it->isHcalDetId())
0136           continue;
0137 
0138         HcalDetId did = HcalDetId(it->rawId());
0139         uint32_t hash = _hashmap.getHash(did);
0140         typename CompactMap::iterator mit = _cmap.find(hash);
0141         if (mit != _cmap.end())
0142           continue;
0143         if (filter.filter(did))
0144           continue;
0145 
0146         _logger.debug(_hashmap.getName(did));
0147 
0148         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0149       }
0150     } else if (_hashmap.isEHash()) {
0151       std::vector<HcalElectronicsId> eids = emap->allElectronicsIdPrecision();
0152       for (std::vector<HcalElectronicsId>::const_iterator it = eids.begin(); it != eids.end(); ++it) {
0153         HcalElectronicsId eid = HcalElectronicsId(it->rawId());
0154         uint32_t hash = _hashmap.getHash(eid);
0155         typename CompactMap::iterator mit = _cmap.find(hash);
0156         if (filter.filter(eid))
0157           continue;
0158         if (mit != _cmap.end())
0159           continue;
0160         _logger.debug(eid);
0161 
0162         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0163       }
0164     } else if (_hashmap.isTHash()) {
0165       std::vector<HcalTrigTowerDetId> tids = emap->allTriggerId();
0166       for (std::vector<HcalTrigTowerDetId>::const_iterator it = tids.begin(); it != tids.end(); ++it) {
0167         HcalTrigTowerDetId tid = HcalTrigTowerDetId(it->rawId());
0168         uint32_t hash = _hashmap.getHash(tid);
0169         typename CompactMap::iterator mit = _cmap.find(hash);
0170         if (mit != _cmap.end())
0171           continue;
0172         if (filter.filter(tid))
0173           continue;
0174         _logger.debug(_hashmap.getName(tid));
0175 
0176         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0177       }
0178     }
0179   }
0180 
0181   template <typename STDTYPE>
0182   void ContainerXXX<STDTYPE>::set(HcalDetId const &did, STDTYPE x) {
0183     _cmap[_hashmap.getHash(did)] = x;
0184   }
0185 
0186   template <typename STDTYPE>
0187   void ContainerXXX<STDTYPE>::set(HcalElectronicsId const &did, STDTYPE x) {
0188     _cmap[_hashmap.getHash(did)] = x;
0189   }
0190 
0191   template <typename STDTYPE>
0192   void ContainerXXX<STDTYPE>::set(HcalTrigTowerDetId const &did, STDTYPE x) {
0193     _cmap[_hashmap.getHash(did)] = x;
0194   }
0195 
0196   template <typename STDTYPE>
0197   STDTYPE &ContainerXXX<STDTYPE>::get(HcalDetId const &did) {
0198     return _cmap[_hashmap.getHash(did)];
0199   }
0200 
0201   template <typename STDTYPE>
0202   STDTYPE &ContainerXXX<STDTYPE>::get(HcalElectronicsId const &eid) {
0203     return _cmap[_hashmap.getHash(eid)];
0204   }
0205 
0206   template <typename STDTYPE>
0207   STDTYPE &ContainerXXX<STDTYPE>::get(HcalTrigTowerDetId const &tid) {
0208     return _cmap[_hashmap.getHash(tid)];
0209   }
0210 
0211   template <typename STDTYPE>
0212   bool ContainerXXX<STDTYPE>::exists(HcalDetId const &id) {
0213     return _cmap.find(id.rawId()) != _cmap.end();
0214   }
0215 
0216   template <typename STDTYPE>
0217   bool ContainerXXX<STDTYPE>::exists(HcalElectronicsId const &id) {
0218     return _cmap.find(id.rawId()) != _cmap.end();
0219   }
0220 
0221   template <typename STDTYPE>
0222   bool ContainerXXX<STDTYPE>::exists(HcalTrigTowerDetId const &id) {
0223     return _cmap.find(id.rawId()) != _cmap.end();
0224   }
0225 
0226   template <typename STDTYPE>
0227   void ContainerXXX<STDTYPE>::push(HcalDetId const &did, STDTYPE x) {
0228     uint32_t hash = did.rawId();
0229     typename CompactMap::iterator mit = _cmap.find(hash);
0230     if (mit != _cmap.end())
0231       return;
0232     _cmap.insert(std::make_pair(hash, x));
0233   }
0234 
0235   template <typename STDTYPE>
0236   void ContainerXXX<STDTYPE>::push(HcalElectronicsId const &eid, STDTYPE x) {
0237     uint32_t hash = eid.rawId();
0238     typename CompactMap::iterator mit = _cmap.find(hash);
0239     if (mit != _cmap.end())
0240       return;
0241     _cmap.insert(std::make_pair(hash, x));
0242   }
0243 
0244   template <typename STDTYPE>
0245   void ContainerXXX<STDTYPE>::push(HcalTrigTowerDetId const &tid, STDTYPE x) {
0246     uint32_t hash = tid.rawId();
0247     typename CompactMap::iterator mit = _cmap.find(hash);
0248     if (mit != _cmap.end())
0249       return;
0250     _cmap.insert(std::make_pair(hash, x));
0251   }
0252 
0253   template <typename STDTYPE>
0254   uint32_t ContainerXXX<STDTYPE>::size() {
0255     return (uint32_t)(_cmap.size());
0256   }
0257 
0258   template <typename STDTYPE>
0259   void ContainerXXX<STDTYPE>::dump(Container1D *c) {
0260     for (auto &p : _cmap) {
0261       STDTYPE &x = p.second;
0262       uint32_t hash = p.first;
0263       c->fill(hash, (double)x);
0264     }
0265   }
0266 
0267   template <typename STDTYPE>
0268   void ContainerXXX<STDTYPE>::dump(std::vector<Container1D *> const &vc) {
0269     for (auto &p : _cmap) {
0270       STDTYPE &x = p.second;
0271       uint32_t hash = p.first;
0272 
0273       for (std::vector<Container1D *>::const_iterator it = vc.begin(); it != vc.end(); ++it)
0274         (*it)->fill(hash, (double)x);
0275     }
0276   }
0277 
0278   template <typename STDTYPE>
0279   void ContainerXXX<STDTYPE>::print() {
0280     std::cout << "Container by " << _hashmap.getHashTypeName() << std::endl;
0281     for (auto &p : _cmap) {
0282       if (_hashmap.isDHash())
0283         std::cout << HcalDetId(p.first) << p.second << std::endl;
0284       else if (_hashmap.isEHash())
0285         std::cout << HcalElectronicsId(p.first) << p.second << std::endl;
0286       else if (_hashmap.isTHash())
0287         std::cout << HcalTrigTowerDetId(p.first) << p.second << std::endl;
0288     }
0289   }
0290 
0291   template <typename STDTYPE>
0292   void ContainerXXX<STDTYPE>::reset() {
0293     for (auto &p : _cmap) {
0294       p.second = 0;
0295     }
0296   }
0297 
0298   template <typename STDTYPE>
0299   void ContainerXXX<STDTYPE>::load(Container1D *cont) {
0300     for (auto &p : _cmap) {
0301       STDTYPE &x = p.second;
0302       uint32_t hash = p.first;
0303 
0304       if (_hashmap.isDHash())
0305         x = cont->getBinContent(HcalDetId(hash));
0306       else if (_hashmap.isEHash())
0307         x = cont->getBinContent(HcalElectronicsId(hash));
0308       else if (_hashmap.isTHash())
0309         x = cont->getBinContent(HcalTrigTowerDetId(hash));
0310     }
0311   }
0312 }  // namespace hcaldqm
0313 
0314 #endif