Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:36:48

0001 #ifndef DEcompare_H
0002 #define DEcompare_H
0003 
0004 /*\class template DEcompare
0005  *\description data|emulation comparison base
0006  *\author Nuno Leonardo (CERN)
0007  *\date 07.03
0008  */
0009 
0010 /*\note specialization free*/
0011 
0012 #include <ostream>
0013 
0014 #include "L1Trigger/HardwareValidation/interface/DEtrait.h"
0015 #include "L1Trigger/HardwareValidation/interface/DEutils.h"
0016 
0017 template <typename T>
0018 struct de_rank;
0019 template <typename T>
0020 struct DEutils;
0021 template <typename T>
0022 struct DEtrait;
0023 
0024 template <typename T>
0025 class DEcompare {
0026   typedef typename T::size_type col_sz;
0027   typedef typename T::const_iterator col_cit;
0028   typedef typename T::iterator col_it;
0029   typedef DEtrait<T> de_trait;
0030   typedef typename de_trait::coll_type coll_type;
0031   typedef typename de_trait::cand_type cand_type;
0032   typedef typename std::vector<cand_type> cand_vec;
0033   typedef typename std::pair<cand_type, cand_type> cand_pair;
0034   typedef typename de_trait::coll_type const* typeT;
0035 
0036 public:
0037   DEcompare() {}
0038   ~DEcompare() {}
0039 
0040   DEcompare(typeT dt, typeT em) : data_(dt), emul_(em), t_match(false) {
0041     ncand_[0] = get_ncand(dt);
0042     ncand_[1] = get_ncand(em);
0043     deDigiColl_.clear();
0044     if (debug_)
0045       LogDebug("DEcompare") << "DEcompare"
0046                             << ": creating instance of type: " << GetName(0) << ", data size:" << data_->size()
0047                             << " ncand:" << ncand_[0] << ", emul size:" << emul_->size() << " ncand:" << ncand_[1]
0048                             << ".\n"
0049                             << std::flush;
0050   };
0051 
0052   //handles constructor (deprecated)
0053   /*
0054   DEcompare(edm::Handle<T> dt, edm::Handle<T> em) : 
0055     data_(dt.product()), emul_(em.product()), t_match(false) {
0056     ncand_[0]=get_ncand(dt.product());
0057     ncand_[1]=get_ncand(em.product());
0058     deDigiColl_.clear();
0059   } 
0060   */
0061 
0062   bool do_compare(std::ostream&, int dump = 0);
0063   bool CompareCollections(std::ostream&, int dump = 0);
0064   bool SortCollections(cand_vec& dg, cand_vec& eg, cand_vec& db, cand_vec& eb);
0065   bool DumpCandidate(col_cit itd, col_cit itm, std::ostream&, int mode = 0);
0066   int get_ncand(typeT) const;
0067   int get_ncand(int i) const {
0068     if (debug_ && (i < 0 || i > 2))
0069       LogDebug("DEcompare") << "DEcompare illegal number of candidates request flag:" << i << "\n";
0070     return ncand_[i];
0071   }
0072 
0073   std::string GetName(int i = 0) const { return de_utils.GetName(i); }
0074   std::string print(col_cit it) const { return de_utils.print(it); }
0075   bool is_empty(col_cit it) const { return de_utils.is_empty(it); }
0076   inline int de_type() const { return de_trait::de_type(); }
0077   bool get_match() const { return t_match; }
0078 
0079   L1DEDigiCollection getDEDigis() const { return deDigiColl_; }
0080 
0081   std::string print() const {
0082     std::stringstream ss("");
0083     for (col_cit itd = data_->begin(); itd != data_->end(); itd++)
0084       if (!is_empty(itd))
0085         ss << "  data: " << print(itd);
0086     for (col_cit itm = emul_->begin(); itm != emul_->end(); itm++)
0087       if (!is_empty(itm))
0088         ss << "  emul: " << print(itm);
0089     return ss.str();
0090   }
0091 
0092 public:
0093   static const int debug_ = 0;
0094 
0095 private:
0096   typeT data_;
0097   typeT emul_;
0098   DEutils<T> de_utils;
0099   bool t_match;
0100   int ncand_[2];
0101   L1DEDigiCollection deDigiColl_;
0102 };
0103 
0104 template <typename T>
0105 bool DEcompare<T>::do_compare(std::ostream& os, int dump) {
0106   if (debug_)
0107     std::cout << " DEcompare::do_compare... " << GetName() << "\n" << std::flush;
0108   t_match = CompareCollections(os, dump);
0109   std::string ok;
0110   if (t_match)
0111     ok = "successful";
0112   else
0113     ok = "failed";
0114   if (dump == -1 || (dump == 1 && !t_match))
0115     os << "  ..." << GetName() << " data and emulator comparison: " << ok.c_str() << std::endl;
0116   return t_match;
0117 }
0118 
0119 template <typename T>
0120 int DEcompare<T>::get_ncand(typeT col) const {
0121   int ncand = 0;
0122   for (col_cit it = col->begin(); it != col->end(); it++) {
0123     if (!is_empty(it)) {
0124       ncand++;
0125     }
0126   }
0127   return ncand;
0128 }
0129 
0130 template <typename T>
0131 bool DEcompare<T>::CompareCollections(std::ostream& os, int dump) {
0132   if (debug_)
0133     std::cout << " DEcompare::CompareCollections...\n" << std::flush;
0134   bool match = true;
0135   int ndata = get_ncand(0);
0136   int nemul = get_ncand(1);
0137   assert(ndata || nemul);
0138 
0139   cand_vec data_good, emul_good, data_bad, emul_bad;
0140   data_good.reserve(data_->size());
0141   emul_good.reserve(emul_->size());
0142   data_bad.reserve(data_->size());
0143   emul_bad.reserve(emul_->size());
0144 
0145   // find matching candidates --  tbd report order match
0146   match &= SortCollections(data_good, emul_good, data_bad, emul_bad);
0147 
0148   if (debug_)
0149     std::cout << "\tDEcompare stats2:"
0150               << " data_bad:" << data_bad.size() << " emul_bad:" << emul_bad.size() << " data_good:" << data_good.size()
0151               << " emul_good:" << emul_good.size() << " data_:" << data_->size() << " emul_:" << emul_->size() << ".\n"
0152               << std::flush;
0153 
0154   if (dump == -1 || (dump == 1 && !match)) {
0155     os << "  number of candidates: " << ndata;
0156     if (ndata != nemul) {
0157       match &= false;
0158       os << " (data) " << nemul << " (emul) disagree";
0159     }
0160     os << std::endl;
0161   }
0162 
0163   col_cit itd, itm;
0164   int prtmode = 0;
0165   col_sz ndt = 0, nem = 0, nde = 0;
0166   deDigiColl_.clear();
0167 
0168   /// ---- treat NON-AGREEING candidates ----
0169 
0170   ndt = data_bad.size();
0171   nem = emul_bad.size();
0172   nde = (ndt > nem) ? ndt : nem;
0173 
0174   itd = data_bad.begin();
0175   itm = emul_bad.begin();
0176 
0177   if (dump == -1)
0178     os << "   un-matched (" << nde << ")\n";
0179 
0180   for (col_sz i = 0; i < nde; i++) {
0181     if (i < ndt && i < nem)
0182       prtmode = 0;  //dt+em
0183     else if (i < ndt && i >= nem)
0184       prtmode = 1;  //dt
0185     else if (i >= ndt && i < nem)
0186       prtmode = 2;  //em
0187     else
0188       assert(0);
0189 
0190     if (abs(dump) == 1)
0191       DumpCandidate(itd, itm, os, prtmode);
0192     else if (prtmode == 0) {
0193       if ((dump == 2 && !de_utils.de_equal_loc(*itd, *itm)) || (dump == 3 && de_utils.de_equal_loc(*itd, *itm)))
0194         DumpCandidate(itd, itm, os, prtmode);
0195     }
0196 
0197     // Fill in DEdigi collection
0198     if (prtmode == 0) {
0199       if (de_utils.de_equal_loc(*itd, *itm)) {
0200         deDigiColl_.push_back(de_utils.DEDigi(itd, itm, 1));
0201       } else {
0202         deDigiColl_.push_back(de_utils.DEDigi(itd, itm, 2));
0203       }
0204     } else if (prtmode == 1) {
0205       deDigiColl_.push_back(de_utils.DEDigi(itd, itd, 3));
0206     } else if (prtmode == 2) {
0207       deDigiColl_.push_back(de_utils.DEDigi(itm, itm, 4));
0208     }
0209 
0210     itd++;
0211     itm++;
0212   }
0213 
0214   /// ---- treat AGREEING candidates ----
0215 
0216   itd = data_good.begin();
0217   itm = emul_good.begin();
0218 
0219   assert(data_good.size() == emul_good.size());
0220   if (dump == -1)
0221     os << "   matched (" << data_good.size() << ")\n";
0222 
0223   for (col_sz i = 0; i < data_good.size(); i++) {
0224     assert(de_utils.de_equal(*itd, *itm));
0225     if (dump == -1)
0226       DumpCandidate(itd, itm, os, prtmode);
0227     deDigiColl_.push_back(de_utils.DEDigi(itd, itm, 0));
0228     itd++;
0229     itm++;
0230   }
0231 
0232   if (debug_)
0233     std::cout << "DEcompare<T>::CompareCollections end.\n" << std::flush;
0234 
0235   return match;
0236 }
0237 
0238 template <typename T>
0239 bool DEcompare<T>::SortCollections(cand_vec& data_good, cand_vec& emul_good, cand_vec& data_bad, cand_vec& emul_bad) {
0240   if (debug_)
0241     std::cout << " DEcompare::SortCollections...\n" << std::flush;
0242 
0243   bool match = true;
0244   cand_vec data_tmp, emul_tmp;
0245 
0246   data_good.clear();
0247   emul_good.clear();
0248   data_bad.clear();
0249   emul_bad.clear();
0250   data_tmp.clear();
0251   emul_tmp.clear();
0252 
0253   for (col_cit ite = emul_->begin(); ite != emul_->end(); ite++) {
0254     if (!is_empty(ite)) {
0255       emul_tmp.push_back(*ite);
0256     }
0257   }
0258 
0259   for (col_cit itd = data_->begin(); itd != data_->end(); itd++) {
0260     if (is_empty(itd))
0261       continue;
0262     col_it ite = emul_tmp.end();
0263     ite = de_utils.de_find(emul_tmp.begin(), emul_tmp.end(), *itd);
0264     if (ite != emul_tmp.end()) {
0265       data_good.push_back(*itd);
0266       emul_good.push_back(*ite);
0267       ite = emul_tmp.erase(ite);
0268     } else {
0269       data_tmp.push_back(*itd);
0270       match &= false;
0271     }
0272   }
0273 
0274   for (col_it itd = data_tmp.begin(); itd != data_tmp.end(); itd++) {
0275     for (col_it ite = emul_tmp.begin(); ite != emul_tmp.end(); ite++) {
0276       if (de_utils.de_equal_loc(*itd, *ite)) {
0277         data_bad.push_back(*itd);
0278         emul_bad.push_back(*ite);
0279         itd = data_tmp.erase(itd) - 1;
0280         ite = emul_tmp.erase(ite) - 1;
0281         break;
0282       }
0283     }
0284   }
0285 
0286   sort(data_tmp.begin(), data_tmp.end(), de_rank<coll_type>());
0287   sort(emul_tmp.begin(), emul_tmp.end(), de_rank<coll_type>());
0288 
0289   data_bad.insert(data_bad.end(), data_tmp.begin(), data_tmp.end());
0290   emul_bad.insert(emul_bad.end(), emul_tmp.begin(), emul_tmp.end());
0291 
0292   if (debug_)
0293     std::cout << "\tDEcompare stats1:"
0294               << " data_bad:" << data_bad.size() << " emul_bad:" << emul_bad.size() << " data_good:" << data_good.size()
0295               << " emul_good:" << emul_good.size() << " data: " << get_ncand(data_) << " emul: " << get_ncand(emul_)
0296               << "\n"
0297               << std::flush;
0298   if (debug_)
0299     std::cout << "DEcompare<T>::SortCollections end.\n" << std::flush;
0300   return match;
0301 }
0302 
0303 template <typename T>
0304 bool DEcompare<T>::DumpCandidate(col_cit itd, col_cit itm, std::ostream& os, int mode) {
0305   if (mode != 2)
0306     os << "   data: " << print(itd);
0307   if (mode != 1)
0308     os << "   emul: " << print(itm) << std::endl;
0309   if (mode == 0)
0310     if (!de_utils.de_equal(*itd, *itm))
0311       return false;
0312   return true;
0313 }
0314 
0315 #endif